Re: Why don't have an object() instance a __dict__ attribute by default?
- From: "Gabriel Genellina" <gagsl-py@xxxxxxxxxxxx>
- Date: Tue, 30 Jan 2007 21:15:55 -0300
En Tue, 30 Jan 2007 20:31:26 -0300, Létező <letezo@xxxxx> escribió:
I use Python 2.5, Win32 MSI release.
Setting attributes on an empty object does not work:
a=object()
Traceback (most recent call last):a.x=1
File "<stdin>", line 1, in <module>
AttributeError: 'object' object has no attribute 'x'
However using an empty new style class works fine:
...class C(object): passc=C()
setattr(c, 'x', 1)
The dir(c) function indicates, that the C instance has a __dict__ attribute.
Why don't have an object() instance a __dict__ attribute by default?
Because it's not needed, and creating an empty dict has some cost. If you need a class with __dict__, just inherit from object as you already have noted.
This may need some explanation in the Python manual.
Maybe.
--
Gabriel Genellina
.
- Prev by Date: Re: Sorting a List of Lists
- Next by Date: Re: Sorting a List of Lists
- Previous by thread: Sorting a List of Lists
- Next by thread: grp.struct_group bug ?
- Index(es):
Relevant Pages
|