[Tutor] object's attributes

Alan Gauld alan.gauld at btinternet.com
Fri Jan 2 17:56:16 CET 2009


"spir" <denis.spir at free.fr> wrote

> Can someone explain the following?

Not really an explanation but I did notice when repeating your
experiment that your class instance has a __dict__ attribute
but object does not. The new attribute a is apparently
located in the dict.

>>> class C(object): pass
...
>>> o = object()
>>> c = C()
>>> c.a = 6
>>> dir(c)
['__class__', '__delattr__', '__dict__', '__doc__', 
'__getattribute__', '__hash_
_', '__init__', '__module__', '__new__', '__reduce__', 
'__reduce_ex__', '__repr_
_', '__setattr__', '__str__', '__weakref__', 'a']
>>> dir(o)
['__class__', '__delattr__', '__doc__', '__getattribute__', 
'__hash__', '__init_
_', '__new__', '__reduce__', '__reduce_ex__', '__repr__', 
'__setattr__', '__str_
_']

>>> print c.__dict__
{'a': 6}

And the dict exists before the new attribuite is created:

>>> d = C()
>>> dir(d)
['__class__', '__delattr__', '__dict__', '__doc__', 
'__getattribute__', '__hash_
_', '__init__', '__module__', '__new__', '__reduce__', 
'__reduce_ex__', '__repr_
_', '__setattr__', '__str__', '__weakref__']

Does that help? I don't know.
Why does a class inheriting from object acquire a dict when object 
doesn't have one?

I'm sure somebody can explain but its not me...

Alan G.




More information about the Tutor mailing list