Inheritance
A class can be built on top of another one. Write class Dog(Animal) and every Dog can use what Animal defines, while Dog adds or replaces only what is different. Step through a ten-line program, pets.py, and watch where Python finds each attribute: __init__ comes from Animal, name lives on the object itself, and Dog's own speak wins over Animal's. That fixed search order is the method resolution order (MRO), and super() lets an override call the parent's version.
5 quick questions
Pick an answer to see why. Your score saves with your progress.
01class Cat(Animal): has no __init__ of its own, and Animal's __init__ sets self.name. What does Cat("Tom").name give?
Python looks for __init__ in Cat, finds none, follows the link to the base class Animal and runs its __init__ on the new Cat object, which stores name = 'Tom'.
02Both Animal and Dog define speak(). rex = Dog("Rex"). Which speak does rex.speak() run?
Python searches the object, then its class Dog, then Animal, and stops at the first match. Dog's speak overrides Animal's.
03After rex = Dog("Rex") runs Animal's __init__ (self.name = name), where is name stored?
self is the new object, so self.name = name sets an instance attribute: rex.__dict__ is {'name': 'Rex'}. Neither class gains a name attribute.
04What does [c.__name__ for c in Dog.__mro__] show for class Dog(Animal) where class Animal: has no brackets?
The MRO lists the classes in search order, starting with Dog itself. A class written without brackets inherits from object, so object comes last.
05Inside Dog's speak, what does super().speak() call?
super() continues the search from the class after the current one in the MRO, so in Dog it finds Animal's speak. It works in any method, not only __init__.
Glossary
- Inheritance
- Building a new class on an existing one: the new class can use everything the existing class defines and add or replace parts.
- Base class (parent)
- The class named in brackets, as Animal in class Dog(Animal). Also called superclass.
- Subclass (child)
- A class that inherits from another, such as Dog. Also called derived class.
- Instance attribute
- A value stored on one object, usually through self, as self.name = name. Each object has its own.
- Override
- A method in a subclass with the same name as one in its base class. Python finds the subclass's version first, so it wins.
- Method resolution order (MRO)
- The order in which Python searches classes for an attribute; Dog.__mro__ lists it, ending with object.
- super()
- A built-in that continues the attribute search after the current class in the MRO, so an override can call its parent's version.
- isinstance() / issubclass()
- Built-ins that check relationships: isinstance(rex, Animal) is True for a Dog; issubclass(Dog, Animal) is True.
Notes and sources
- A class written without brackets, such as class Animal:, inherits from object; it is the same as class Animal(object):. So every class has object at the end of its MRO, and that is where methods such as __repr__ come from.
- For special methods such as __init__, Python looks on the type of the object, not on the object itself, so the figure starts that search at Dog. For ordinary attributes such as name and speak it looks on the object first.
- A subclass does not copy its base class. Dog.__dict__ holds only speak; when Python cannot find an attribute in Dog it follows Dog.__bases__ to Animal. Add a method to Animal later and every Dog can use it at once.
- A class can have several base classes, class C(A, B):. Python then orders them with the C3 algorithm so that each class comes before its bases and the order in brackets is kept; C.__mro__ shows the result.
- python -i runs the file and then stays at the >>> prompt with the program's names still defined, which is how the figure asks for Dog.__mro__ at the end. The 0x... address in a function's repr varies from run to run.
- Python tutorial: Classes, Inheritance
- Python tutorial: Class objects and instantiation (__init__)
- Python tutorial: Multiple inheritance
- Python reference: Class definitions (default base class object)
- Python reference: Data model, custom classes and class instances
- Python reference: Special method lookup
- Python reference: type.__mro__
- Python library: Built-in functions, super()
- Python library: Built-in functions, isinstance()
- Python HOWTO: The Python 2.3 method resolution order (C3)
- Python setup and usage: Command line, the -i option
Quiz Unlocks after the last step.