为什么不能在python中向对象添加属性?

时间:2009-08-16 20:26:54

标签: 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
>>> 

为什么不允许对象添加属性?

2 个答案:

答案 0 :(得分:43)

请注意,object实例没有__dict__属性:

>>> dir(object())
['__class__', '__delattr__', '__doc__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__']

在派生类中说明此行为的示例:

>>> 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'

引用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__'

你会注意到有__dict__个实例。对象类可能没有定义__slots__,但结果是相同的:缺少__dict__,这是阻止动态分配属性的原因。我重新组织了我的回答,使其更清晰(将第二段移到顶部)。

答案 1 :(得分:3)

好问题,我的猜测是,它与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'

IIRC,这与__dict__属性的存在有关,或者更确切地说,当对象没有setattr()属性时,__dict__会爆炸。