[issue7968] __dict__ Exception using class attributes

Georg Brandl report at bugs.python.org
Sat Feb 20 22:33:36 CET 2010


Georg Brandl <georg at python.org> added the comment:

> That seems a little strange to me, because if an object is instance of
> a class, it should inherit all of it attributes.

An instance does not "inherit" attributes of its class.  Instead, when
looking up an attribute for an instance, attributes of the class (most
prominently methods) are considered after looking in the instance
__dict__.

> Is it an optimization issue? because I observed that all
> instances of a class that has such "static" attributes, shares that
> attributes for less memory using.

No, this has nothing to do with optimization.  The main point, which
may be surprising if you come from a background of more static
languages, is that in Python, the class itself is an object of its own
right, and can have attributes (it has its own __dict__).  Therefore,
class attributes exist only once, and appear to be "shared" when
accessed through a class instance.

----------
nosy: +georg.brandl

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue7968>
_______________________________________


More information about the Python-bugs-list mailing list