This section assumes you already understand the basics of object-oriented programming in Python, and that you know the meaning of concepts such as class, instance, method, and attribute. For a general tutorial on these concepts, see the introduction to object-oriented Python programming in the Tech Computer Center's Python tutorial.
Here is the general form of the class declaration for some
class with
one or more parent classes C, P1, …:
P2
classC(P1,P2, ...):attribute definitions...
To declare a class that does not inherit from any parent classes:
classC:attribute definitions...
The may include any number of attribute definitionsdef
blocks that declare methods of the class, and any number
of class variable declarations.
Functionally, a class is really just a namespace. This namespace is just a place to store the pieces of the class mechanisms: its methods and class variables.
When Python reads a “class” declaration, it creates a new, empty
namespace.
When Python reads a “def” within a class, the name of that method is added to the class's namespace.
If you define a class variable (that is, if you assign a value to a name inside a class but outside of any methods of the class), the class variable's name and value are added to the namespace.
A brief conversational session may serve to illustrate
these concepts. We'll make use of the built-in function
dir() to show the contents of the class's
namespace; see Section 21.5, “dir(): Display a namespace's names”.
>>> class Taunter:... tauntCount = 0
... def taunt(self):
... print "Go away, or I shall taunt you a second time!" ... >>> dir(Taunter)
['__doc__', '__module__', 'taunt', 'tauntCount'] >>> type(Taunter.__doc__) <type 'NoneType'> >>> Taunter.__module__ '__main__' >>> Taunter.tauntCount
0 >>> Taunter.taunt
<unbound method Taunter.taunt>
An instance of a class is also a namespace. When the instance is created, all the names from the class's namespace are copied into the instance namespace. From that point on, any changes made to the instance's namespace do not affect the class namespace:
>>> frenchy = Taunter()>>> dir(frenchy) ['__doc__', '__module__', 'taunt', 'tauntCount'] >>> frenchy.where = 'crenelations'
>>> dir(frenchy)
['__doc__', '__module__', 'where', 'taunt', 'tauntCount'] >>> frenchy.where 'crenelations' >>> dir(Taunter) ['__doc__', '__module__', 'taunt', 'tauntCount'] >>> frenchy.tauntCount
0 >>> frenchy.tauntCount += 1
>>> frenchy.tauntCount 1 >>> Taunter.tauntCount 0 >>> type(frenchy.taunt)
<type 'instancemethod'> >>> frenchy.taunt()
Go away, or I shall taunt you a second time! >>> Taunter.taunt(frenchy)
Go away, or I shall taunt you a second time!
Namespaces are very much like dictionaries. Where a
dictionary has unique keys, a namespace has unique names.
As a matter of fact, classes and instances have a special
built-in attribute called “__dict__” which, for most purposes, is the namespace as a dictionary. Continuing the examples
above:
>>> Taunter.__dict__
{'taunt': <function taunt at 0xb7ed002c>, '__module__': '__main__', 'tau
ntCount': 0, '__doc__': None}
>>> newFrenchy=Taunter()
>>> newFrenchy.__dict__
{}
>>> frenchy.__dict__
{'tauntCount': 1, 'where': 'crenelations'}
The class's dictionary has the four names we expect: the
built-ins __module__ and __doc__, the class variable tauntCount, and the method taunt.
But notice that the __dict__ attribute of
the newly created instance newFrenchy does
not have the four names copied from the class. In fact, it
is empty. And the __dict__ of instance
frenchy contains only the names that have
changed since its instantation.
What actually happens when you refer to an attribute is
that Python looks first in the instance's __dict__; if the name is not found there, it looks
in the __dict__ of the class. For derived
classes, Python will also search the __dict__ attributes of all the ancestor classes.
So, in our example, a reference to frenchy.tauntCount would find the value of 1 in
the instance. A reference to newFrenchy.tauntCount would fail to find that name
in newFrench.__dict__, but would succeed in
finding the class variable value 0 in Taunter.__dict__['tauntCount'].
Let's now look at the life cycles of classes in more detail. Due to improvements made in the language since it was first introduced, Python has two kinds of classes, old-style and new-style. We encourage you to use new-style classes; old-style classes will no longer be supported in the next major release, Python 3000.
All new-style classes must declare at least one parent class
that is either the top-level class object or
some other class that derives ultimately from object. Such a class is said to be derived from, or inherits
from, the object class.
To declare a new-style class C
that inherits from object:
classC(object): ...class methods and variables...
An old-style class is one that doesn't declare a parent class at all, or a class that inherits from an existing old-style class. The life cycle of an old-style class is described in Section 26.1, “Old-style classes”.
In most respects, the two classes perform identically.
We'll start by explaining old-style classes in Section 26.1, “Old-style classes”.
To benefit from the many functional improvements of new-style classes, and especially if you expect to migrate your code to the major changes of Python 3.0, see Section 26.2, “Life cycle of a new-style class”.