Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
407 views
in Technique[技术] by (71.8m points)

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

From https://docs.python.org/3.3/library/functions.html#object

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

Why "object does not have a __dict__"? It works for me

>>> object.__dict__
mappingproxy({'__repr__': <slot wrapper '__repr__' of 'object' objects>, '__hash__': <slot wrapper '__hash__' of 'object' objects>, '__subclasshook__': <method '__subclasshook__' of 'object' objects>, '__ne__': <slot wrapper '__ne__' of 'object' objects>, '__format__': <method '__format__' of 'object' objects>, '__new__': <built-in method __new__ of type object at 0xa3dc20>, '__doc__': 'The most base type', '__class__': <attribute '__class__' of 'object' objects>, '__dir__': <method '__dir__' of 'object' objects>, '__delattr__': <slot wrapper '__delattr__' of 'object' objects>, '__getattribute__': <slot wrapper '__getattribute__' of 'object' objects>, '__le__': <slot wrapper '__le__' of 'object' objects>, '__init__': <slot wrapper '__init__' of 'object' objects>, '__gt__': <slot wrapper '__gt__' of 'object' objects>, '__ge__': <slot wrapper '__ge__' of 'object' objects>, '__eq__': <slot wrapper '__eq__' of 'object' objects>, '__reduce__': <method '__reduce__' of 'object' objects>, '__lt__': <slot wrapper '__lt__' of 'object' objects>, '__str__': <slot wrapper '__str__' of 'object' objects>, '__reduce_ex__': <method '__reduce_ex__' of 'object' objects>, '__sizeof__': <method '__sizeof__' of 'object' objects>, '__setattr__': <slot wrapper '__setattr__' of 'object' objects>})

Why "you can’t assign arbitrary attributes to an instance of the object class"?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You are confusing the __dict__ on the type with the attribute on instances. object() instances do not have a __dict__ attribute:

 >>> object().__dict__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'object' object has no attribute '__dict__'

Note that the __dict__ attribute of custom Python class instances is a descriptor; the instance itself doesn't have the attribute, it is the class that provides it (so type(instance).__dict__['__dict__'].__get__(instance) is returned). object.__dict__ may exist, but object.__dict__['__dict__'] does not.

object() doesn't support instance attributes because it is the base for all custom Python classes, which must support not having a __dict__ attribute when defining slots instead.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...