gpt4 book ai didi

python - 为什么对象类不允许属性?

转载 作者:行者123 更新时间:2023-11-28 17:50:15 25 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
Can set any property of Python object

In [67]: obj = object()    
In [68]: obj.x = 42
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
/home/user/<ipython-input-68-0203a27904ca> in <module>()
----> 1 obj.x = 42

AttributeError: 'object' object has no attribute 'x'
In [69]: class myc(object):
....: pass
....:
In [70]: my = myc()
In [71]: my.x = 42

看起来像一个不一致。如何在不允许添加新属性的情况下使我的类类似于 object

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com