11.5 Inheritance
Of course, a language feature would not be worthy of the name "class"
without supporting inheritance. The syntax for a derived class
definition looks as follows:
class DerivedClassName(BaseClassName):
<statement-1>
.
.
.
<statement-N>
The name BaseClassName must be defined in a scope containing
the derived class definition. Instead of a base class name, an
expression is also allowed. This is useful 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 object is constructed, the base class is
remembered. This is used for resolving attribute references: if a
requested attribute is not found in the class, it is searched for in the
base class. This rule is applied recursively if the base class itself
is derived from some other class.
There's nothing special about instantiation of derived classes:
DerivedClassName() creates a new instance of the class. Method
references are resolved as follows: the corresponding class attribute
is searched for, descending down the chain of base classes if necessary,
and the method reference is valid if this yields a function object.
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 another method
defined in the same base class, may in fact 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 defined or imported directly in the global scope.)
|