Fre*_*Foo 72 python properties
我已成功使用Python属性,但我不知道它们是如何工作的.如果我取消引用类之外的属性,我只得到一个类型的对象property:
@property
def hello(): return "Hello, world!"
hello  # <property object at 0x9870a8>
但是如果我在一个类中放置一个属性,那么行为就会大不相同:
class Foo(object):
   @property
   def hello(self): return "Hello, world!"
Foo().hello # 'Hello, world!'
我注意到unbound Foo.hello仍然是property对象,所以类实例化必须做出魔法,但那有什么神奇之处呢?
Tim*_*tes 50
正如其他人所说,他们使用称为描述符的语言功能.
通过类访问实际属性对象时返回的原因在于属性Foo.hello如何实现__get__(self, instance, owner)特殊方法:
owner是该实例的类.instance则为None且仅owner传递.该property对象认识到这一点并返回self.除了Descriptors howto之外,另请参阅语言指南中有关实现描述符和调用描述符的文档.
Tim*_*ann 22
为了使@properties正常工作,该类需要是对象的子类.当类不是对象的子类时,第一次尝试访问setter时,它实际上会生成一个名称较短的新属性,而不是通过setter访问.
以下不能正常工作.
class C(): # <-- Notice that object is missing
    def __init__(self):
        self._x = None
    @property
    def x(self):
        print 'getting value of x'
        return self._x
    @x.setter
    def x(self, x):
        print 'setting value of x'
        self._x = x
>>> c = C()
>>> c.x = 1
>>> print c.x, c._x
1 0
以下将正常工作
class C(object):
    def __init__(self):
        self._x = None
    @property
    def x(self):
        print 'getting value of x'
        return self._x
    @x.setter
    def x(self, x):
        print 'setting value of x'
        self._x = x
>>> c = C()
>>> c.x = 1
setting value of x
>>> print c.x, c._x
getting value of x
1 1
| 归档时间: | 
 | 
| 查看次数: | 39547 次 | 
| 最近记录: |