Python中的哪些对象可以动态添加属性?


Python中,我可以向我之前定义的类C添加属性.但是,我无法向列表添加属性 – 生成的错误消息说明这是因为list是内置类型:

>>> class C: pass
...
>>> C.foo = 1
>>> C.foo
1

>>> list.foo = 1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can't set attributes of built-in/extension type 'list'

类似地,可以将属性添加到C的实例,但不能添加到列表的实例.但是,在这种情况下,错误消息更加模糊:

>>> o = C()
>>> o.bar = 2
>>> o.bar
2

>>> o = []
>>> o.bar = 2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'list' object has no attribute 'bar'

为什么我不能将成员添加到列表实例中?是不是因为列表是内置类型?

更一般地说,Python中的哪些对象可以动态添加属性?

最佳答案 在对象上设置任意属性时,这会有效地改变对象的__dict__. __dict__是一个存储对象所有成员的字典.因此,为了向对象添加成员,有两个条件:

>对象需要__dict__.
>对象的__dict__需要是可变的.

有多种原因导致其中一种情况可能不正确.以下类别的类型通常不允许对象修改:

>内置本机类型在本机代码中实现,并具有固定的成员集,并附加到其本机对象定义中.示例包括int,str,list,bytes,…可能大多数(但不是全部)built-in types
>来自外部库的本机类型,例如numpy stuff.
>任何使用__slots__的Python类.插槽是一种明确的方式来转换__dict__并用一组固定的成员替换它.很明显,这将阻止以后添加到对象(每个设计).

检测是否可以修改对象有哪些好方法?好吧,检查上面的条件.对象是否具有__dict__:

>>> class Example:
        pass
>>> class SlotsExample:
        __slots__ = ['x']

>>> hasattr(Example(), '__dict__')
True
>>> hasattr(SlotsExample(), '__dict__')
False
>>> hasattr(list, '__dict__')
True
>>> hasattr([], '__dict__')
False

__dict__是否是真正的字典:

>>> isinstance(Example().__dict__, dict)
True
>>> isinstance(list.__dict__, dict)
False
点赞