我有一个这样的课:
class Foo(object):
def __init__(self):
self.bar = property(self.get_bar)
def get_bar(self):
return "bar"
print Foo().bar #this prints <property object at 0x0051CB40>
我看过Python属性如何工作?,如何在__init__中设置python属性,但是它们都使用了decorator方法,我不需要,因为我想要一个不同的名称。我需要访问self
我如何获得财产的行为?
您需要做些小改动:
class Foo(object):
def get_bar(self):
return "bar"
bar = property(get_bar)
print Foo().bar # prints bar
该属性必须是该类的属性,而不是实例。这就是描述符协议的工作方式。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句