About dynamic assignment of attributes in Python 2.x

后端 未结 1 838
北海茫月
北海茫月 2021-01-14 10:54

When I try to dynamically add attributes to instances of object class, I get an AttributeError. However, it is possible do it with instances of subclasses of object. Does an

1条回答
  •  说谎
    说谎 (楼主)
    2021-01-14 11:37

    There is a note in the documentation about that:

    http://docs.python.org/3/library/functions.html#object

    Note: object does not have a __dict__, so you can’t assign arbitrary attributes to an instance of the object class.

    There is also a discussion about this on python mailing list:

    https://mail.python.org/pipermail/python-list/2011-October/614249.html

    0 讨论(0)
提交回复
热议问题