inheritance
inheritance
Of course, a language feature would not be worthy of the name “class” without supporting inher-
itance. The syntax for a derived class definition looks like this:
class DerivedClassName(BaseClassName):
<statement-1>
.
.
.
<statement-N>
The name BaseClassName must be defined in a namespace accessible from the scope containing
the derived class definition. In place of a base class name, other arbitrary expressions are also
allowed. This can be useful, for example, when the base class is defined in another module:
class DerivedClassName(modname.BaseClassName):
Execution of a derived class definition proceeds the same as for a base class. When the class ob-
ject is constructed, the base class is remembered. This is used for resolving attribute references:
if a requested attribute is not found in the class, the search proceeds to look in the base class.
This rule is applied recursively if the base class itself is derived from some other class.
Derived classes may override methods of their base classes. Because methods have no special
privileges when calling other methods of the same object, a method of a base class that calls an-
other method defined in the same base class may end up calling a method of a derived class
that overrides it. (For C++ programmers: all methods in Python are effectively virtual.)
An overriding method in a derived class may in fact want to extend rather than simply replace
the base class method of the same name. There is a simple way to call the base class method
directly: just call BaseClassName.methodname(self, arguments). This is occasionally useful to
clients as well. (Note that this only works if the base class is accessible as BaseClassName in the
global scope.)
Python supports a form of multiple inheritance as well. A class definition with multi-
ple base classes looks like this:
In fact, it is slightly more complex than that; the method resolution order changes dy-
namically to support cooperative calls to super(). This approach is known in some
other multiple-inheritance languages as call-next-method and is more powerful than
the super call found in single-inheritance languages.
Dynamic ordering is necessary because all cases of multiple inheritance exhibit one or
more diamond relationships (where at least one of the parent classes can be accessed
through multiple paths from the bottommost class). For example, all classes inherit
from object, so any case of multiple inheritance provides more than one path to
reach object. To keep the base classes from being accessed more than once, the dy-
namic algorithm linearizes the search order in a way that preserves the left-to-right or-
dering specified in each class, that calls each parent only once, and that is monotonic
(meaning that a class can be subclassed without affecting the precedence order of its
parents). Taken together, these properties make it possible to design reliable and ex-
tensible classes with multiple inheritance. For more detail, see The Python 2.3
Method Resolution Order.