【发布时间】:2018-02-03 20:13:24
【问题描述】:
我正在尝试理解python中的描述符,我注意到的一件事是:
In [1]: class descriptor:
...: def __init__(self):
...: pass
...: def __get__(self, instance, owner):
...: print ("__get__ called")
...: return 0
...: def __set__(self, obj, value):
...: print ("__set__ called")
In [2]: class Foo:
...: y = descriptor()
...: def __init__(self):
...: self.x = descriptor()
In [3]: Foo.y
__get__ called
Out[3]: 0
In [4]: f = Foo()
In [5]: f.x
Out[5]: <__main__.descriptor at 0x1099dd588>
如您所见,在类属性上,描述符的__get__ 被正确调用,但在实例属性上却没有调用所需的方法。我尝试阅读this,但目前还不清楚该页面的哪一部分适用于此处。
【问题讨论】:
标签: python python-3.6 propertydescriptor