Skip to content Skip to sidebar Skip to footer

"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

Solution 1:

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 calllast):
  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.

Post a Comment for ""object Does Not Have A __dict__, So You Can’t Assign Arbitrary Attributes To An Instance Of The Object Class.""