confirmation = property(_get_confirmation, _set_confirmation)
confirmation.short_description = "Confirmation"
当我尝试上面的操作时,我得到了一个我不太明白的异常:
AttributeError: 'property' object has no attribute 'short_description'
这是一个 answer到这里的另一个问题,但我无法对此发表评论,因为我没有足够的积分或其他东西。 :-(
在其他测试中,我在类似情况下也遇到了这个错误:
TypeError: 'property' object has only read-only attributes (assign to .short_description)
有什么想法吗?
最佳答案
property() 的结果是一个您不能添加新字段或方法的对象。它是不可变的,这就是您收到错误的原因。
实现您想要的效果的一种方法是使用 property()
的四个参数:
confirmation = property(_get_confirmation, _set_confirmation, None, "Confirmation.")
或者将解释放在_get_confirmation
的docstring中。
( see docs here ,Python 2 也支持)
[编辑] 至于 answer ,你指的是:我觉得例子的缩进你看的时候完全错了。现在已修复此问题。
关于Python "property object has no attribute"异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/520152/