【发布时间】:2018-02-04 14:21:06
【问题描述】:
当我将 property 装饰器应用于类定义中的方法时,python 接受带有 @decorator 但不带有显式语法的版本。以下是代码:
>>> class Person:
... first_name = property()
... def first_name(self):
... pass
... first_name = first_name.getter(first_name)
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 5, in Person
AttributeError: 'function' object has no attribute 'getter'
这个结果我能理解——虽然first_name被定义为property对象,然后它被重新定义为一个没有getter方法的函数对象。
但是接下来的代码是如何工作的-
>>> class Person:
... first_name = property()
... @first_name.getter
... def first_name(self):
... pass
...
>>>
我只做了语法上的改变,但这个版本没有错误。为什么?
【问题讨论】:
标签: python python-3.x properties