|
Original |
Translation |
|
107
|
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 end up calling a method of a derived class that overrides it. (For C++ programmers: all methods in Python are effectively ``virtual``.)
|
Les classes dérivées peuvent surcharger des méthodes de leurs classes de base. Comme les méthodes n'ont aucun privilège particulier quand elles appellent d'autres méthodes d'un même objet, une méthode d'une classe de base qui appelle une autre méthode définie dans la même classe peut en fait appeler une méthode d'une classe dérivée qui la surcharge. (Pour les programmeurs C++ : toutes les méthodes de Python sont en effet ``virtual``.)
|
|
108
|
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.)
|
|
|
109
|
Python has two built-in functions that work with inheritance:
|
|
|
110
|
Use :func:`isinstance` to check an instance's type: ``isinstance(obj, int)`` will be ``True`` only if ``obj.__class__`` is :class:`int` or some class derived from :class:`int`.
|
|
|
111
|
|
112
|
|
|
|
113
|
Python supports a form of multiple inheritance as well. A class definition with multiple base classes looks like this::
|
Python propose également une forme d'héritage multiple. Une définition de classe ayant plusieurs classes de base ressemble à ::
|
|
114
|
class DerivedClassName(Base1, Base2, Base3): <statement-1> . . . <statement-N>
|
class NomDeLaClasseDerivee(Base1, Base2, Base3): <instruction-1> . . . <instruction-N>
|
|
115
|
For most purposes, in the simplest cases, you can think of the search for attributes inherited from a parent class as depth-first, left-to-right, not searching twice in the same class where there is an overlap in the hierarchy. Thus, if an attribute is not found in :class:`DerivedClassName`, it is searched for in :class:`Base1`, then (recursively) in the base classes of :class:`Base1`, and if it was not found there, it was searched for in :class:`Base2`, and so on.
|
Dans la plupart des utilisations, pour les cas les plus simples, vous pouvez vous représenter la recherche des attributs hérités depuis une classe parente sous forme en profondeur d'abord et de gauche à droite, sans recherche en double lorsqu'il y a recouvrement dans la hiérarchie des classes. Ainsi, si un attribut n'est pas trouvé dans :class:`NomDeLaClasseDerivee`, il est recherché dans :class: `Base1`,
|
|
116
|
In fact, it is slightly more complex than that; the method resolution order changes dynamically to support cooperative calls to :func:`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.
|
|