Accessing class variables via instance

Refs the Classes and Class instances parts in http://docs.python.org/reference/datamodel.html

A class has a namespace implemented by a dictionary object. Class attribute references are translated to lookups in this dictionary, e.g., C.x is translated to C.__dict__["x"] (although for new-style classes in particular there are a number of hooks which allow for other means of locating attributes)

A class instance is created by calling a class object (see above). A class instance has a namespace implemented as a dictionary which is the first place in which attribute references are searched. When an attribute is not found there, and the instance’s class has an attribute by that name, the search continues with the class attributes.

Generally, this usage is fine, except the special cases mentioned as "new-style classes in particular there are a number of hooks which allow for other means of locating attributes".


Not only can you rely on this behavior, you constantly do.

Think about methods. A method is merely a function that has been made a class attribute. You then look it up on the instance.

>>> def foo(self, x):
...     print "foo:", self, x
... 
>>> class C(object):
...     method = foo # What a weird way to write this! But perhaps illustrative?
... 
>>> C().method("hello")
foo: <__main__.C object at 0xadad50> hello

In the case of objects like functions, this isn't a plain lookup, but some magic occurs to pass self automatically. You may have used other objects that are meant to be stored as class attributes and looked up on the instance; properties are an example (check out the property builtin if you're not familiar with it.)

As okm notes, the way this works is described in the data model reference (including information about and links to more information about the magic that makes methods and properties work). The Data Model page is by far the most useful part of the Language Reference; it also includes among other things documentation about almost all the __foo__ methods and names.