为了说明问题,请检查以下代码:
class MyDescriptor(object):
def __get__(self, obj, type=None):
print "get", self, obj, type
return self._v
def __set__(self, obj, value):
self._v = value
print "set", self, obj, value
return None
class SomeClass1(object):
m = MyDescriptor()
class SomeClass2(object):
def __init__(self):
self.m = MyDescriptor()
x1 = SomeClass1()
x2 = SomeClass2()
x1.m = 1000
# -> set <__main__.MyDescriptor object at 0xb787c7ec> <__main__.SomeClass1 object at 0xb787cc8c> 10000
x2.m = 1000 # I guess that this overwrites the function. But why?
# ->
print x1.m
# -> get <__main__.MyDescriptor object at 0xb787c7ec> <__main__.SomeClass1 object at 0xb787cc8c> <class '__main__.SomeClass1'> 10000
print x2.m
# -> 10000
- 为什么x2.m = 1000不调用__set__ -函数?似乎这会覆盖该功能。但为什么?
- x1中的_v在哪里?它不在x1._v中