Why don't have an object() instance a __dict__ attribute by default?

Létező letezo at fw.hu
Tue Jan 30 18:31:26 EST 2007


I use Python 2.5, Win32 MSI release.

Setting attributes on an empty object does not work:

>>> a=object()

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

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

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

The dir(a) function indicates, that the object instance has no __dict__ attribute.

However using an empty new style class works fine:

>>> class C(object): pass
...
>>> c=C()
>>> setattr(c, 'x', 1)
>>>

The dir(c) function indicates, that the C instance has a __dict__ attribute.

Python 2.5 manual says:

"Return a new featureless object. object is a base for all new style classes. It has the methods that are common to all instances of new style classes."

Why don't have an object() instance a __dict__ attribute by default?

This may need some explanation in the Python manual.

Viktor
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20070131/4c9af8d9/attachment.html>


More information about the Python-list mailing list