__dict__ attribute for built-in types

Arnaud Delobelle arnodel at gmail.com
Thu Oct 27 06:36:54 EDT 2011


On 27 October 2011 11:08, candide <candide at free.invalid> wrote:
> I realize that built-in types objects don't provide a __dict__ attribute and
> thereby i can't set an attribute to a such object, for instance
>
>
>>>> a=[42,421]
>>>> a.foo="bar"
> Traceback (most recent call last):
>  File "<stdin>", line 1, in <module>
> AttributeError: 'list' object has no attribute 'foo'
>>>> a.__dict__
> Traceback (most recent call last):
>  File "<stdin>", line 1, in <module>
> AttributeError: 'list' object has no attribute '__dict__'
>>>>

Some built in types have a __dict__:

>>> def foo(): pass
...
>>> foo.__dict__
{}
>>> import random
>>> len(random.__dict__)
57

>
> So, i was wondering :
>
> -- why this behaviour ?

Performance reasons I guess.

> -- where the official documentation refers to this point ?

I don't know this one :)

-- 
Arnaud



More information about the Python-list mailing list