__dict__ attribute for built-in types

Christian Heimes lists at cheimes.de
Fri Oct 28 07:51:33 EDT 2011


Am 28.10.2011 10:01, schrieb Steven D'Aprano:
>>>> hasattr(int, '__dict__')  # Does the int class/type have a __dict__?
> True
>>>> hasattr(42, '__dict__')  # Does the int instance have a __dict__?
> False

Also __dict__ doesn't have to be an instance of __dict__. Builtin types
usually have a dictproxy instane as their __dict__.

>>> type(int.__dict__)
<type 'dictproxy'>
>>> int.__dict__["egg"] = "spam"
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'dictproxy' object does not support item assignment




More information about the Python-list mailing list