qua*_*ano 62 python attributes instances
(用Python shell编写)
>>> o = object()
>>> o.test = 1
Traceback (most recent call last):
File "<pyshell#45>", line 1, in <module>
o.test = 1
AttributeError: 'object' object has no attribute 'test'
>>> class test1:
pass
>>> t = test1()
>>> t.test
Traceback (most recent call last):
File "<pyshell#50>", line 1, in <module>
t.test
AttributeError: test1 instance has no attribute 'test'
>>> t.test = 1
>>> t.test
1
>>> class test2(object):
pass
>>> t = test2()
>>> t.test = 1
>>> t.test
1
>>>
Run Code Online (Sandbox Code Playgroud)
为什么对象不允许您向其添加属性?
ars*_*ars 47
请注意,object实例没有__dict__属性:
>>> dir(object())
['__class__', '__delattr__', '__doc__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__']
Run Code Online (Sandbox Code Playgroud)
举例说明派生类中的此行为:
>>> class Foo(object):
... __slots__ = {}
...
>>> f = Foo()
>>> f.bar = 42
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'Foo' object has no attribute 'bar'
Run Code Online (Sandbox Code Playgroud)
引用文档slots:
[...]
__slots__声明采用一系列实例变量,并在每个实例中保留足够的空间来保存每个变量的值.保存空间是因为__dict__没有为每个实例创建空间.
编辑:要从评论中回答ThomasH,OP的测试类是一个"旧式"类.尝试:
>>> class test: pass
...
>>> getattr(test(), '__dict__')
{}
>>> getattr(object(), '__dict__')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'object' object has no attribute '__dict__'
Run Code Online (Sandbox Code Playgroud)
你会注意到有一个__dict__例子.对象类可能没有__slots__定义,但结果是相同的:缺少a __dict__,这是阻止属性动态分配的原因.我已经重新组织了我的答案以使其更清楚(将第二段移到顶部).
object好问题,我的猜测是它与内置/扩展类型有关。
>>> class test(object):
... pass
...
>>> test.test = 1
>>> object.test = 1
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can't set attributes of built-in/extension type 'object'
Run Code Online (Sandbox Code Playgroud)
IIRC,这与属性的存在有关__dict__,或者更正确地说,setattr()当对象没有__dict__属性时会爆炸。
| 归档时间: |
|
| 查看次数: |
27925 次 |
| 最近记录: |