Python "property object has no attribute"异常

标签 python django properties

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/

相关文章:

Django 更新 View 集

django - allauth with crispyform : signup not working

python - 在 lambda 中调用属性 setter

python - GAE 开发服务器上的 SMTP 电子邮件 - 如何? Python 2.5.4、GAE SDK 1.7.4

python - 原始 sql 中的 sqlalchemy 日期过滤器

python - Heroku ---> 安装 pip 远程 : AttributeError: module 'pip._vendor.requests' has no attribute 'Session'

javascript - 有没有办法在JS中将 "subproperties"添加到变量中?

python - 在 python 中获取 xscreensaver 状态

python - 将 CSR 矩阵乘以向量

c++ - Q_PROPERTY : MEMBER vs READ/WRITE