When you create a Python application there is one thing that can make your life a lot easier: class inheritance. Let’s learn how to use it.
Class Inheritance allows to create classes based on other classes with the aim of reusing Python code that has already been implemented instead of having to reimplement similar code.
The first two concepts to learn about Python inheritance are the Parent class and Child class.
What is a Parent class?
A Parent class is a class you inherit from in your program to reuse its code. It’s also called Base class or Superclass.
What is a Child class?
The Child class is the class that inherits from the Parent class. It’s also called Derived class or Subclass.
We will work on a simple football game and show how inheritance works in Python.
But first let’s start with some super important inheritance basics!
Difference Between a Parent and a Child Class
We have talked about Parent and Child classes…
What’s the difference between these two when it comes to the way we define them?
Let’s start by defining a class called A in the way we define any class in Python. To simplify this example we will just use the pass statement in the implementation of each class.
What is the pass statement in a Python class?
The pass statement is used in Python classes to define a class without implementing any code in it (e.g. attributes and methods). Using the pass statement is a common technique to create the structure of your program and avoid errors raised by the interpreter due to missing implementation in a class.
I’m using the pass statement because I don’t want you to focus on the code of the classes right now but just on the concept of inheritance itself.
class A:
pass
Class A is just a normal class.
What error do we see if we don’t include the pass statement in our class?
$ python inheritance.py
File "inheritance.py", line 2
^
SyntaxError: unexpected EOF while parsing
The Python interpreter doesn’t like code that only contains the first line of the class A definition without pass.
So, going back to our working example. What makes class A a Parent class?
The fact that in our program we create a class called B that inherits (or derives) from it:
class B(A):
pass
Notice that after the name of the class B I have also included class A within parentheses. This means that B inherits from A. In other words B is a child class and A is its parent class.
But this doesn’t end here…
We can define a class called C that inherits from B:
class C(B):
pass
You can see that the role of a class when it comes to inheritance is not set in stone…what do I mean?
A class can be both a parent and child class like we have seen with class B in our example.
This is very powerful and it allows you to create complex Python programs with very few lines of code.
And now, let’s have a look at a practical example of inheritance.
A First Example of Class Inheritance in Python
Firstly, we create a base class called Player. Its constructor takes a name and a sport:
class Player:
def __init__(self, name, sport):
self.name = name
self.sport = sport
We could use the Player class as Parent class from which we can derive classes for players in different sports. Let’s create child classes that represent football players.
I will define four child classes for the four football roles: goalkeeper, defender, midfielder and striker.
As we have seen before, here is how to create a class in Python that inherits from another class. We use the class statement and additionally we specify the name of the class we want to inherit from after the name of our class within parentheses:
class ChildClass(ParentClass):
...
...
Even if we don’t define any methods in our child class we can still call the methods from the parent class as if they were implemented in the child one. This is the power of inheritance.
We will define our child classes is such a way that the sport attribute is automatically set. Here is an example for the Goalkeeper class:
class Goalkeeper(Player):
def __init__(self, name):
super().__init__(name, 'football')
As you can see, the parent class Player is between parentheses.
Then we define the __init__ method (the constructor) that replaces the __init__ method inherited from the parent class.
If the __init__ method is not defined in a child class then the __init__ method from the parent class is automatically used.
In the constructor we use the method super() that refers to the parent class. We use it to call the constructor of the parent class and we pass to it:
- The name of the player provided when an object of type Goalkeeper is created.
- The ‘football’ sport.
The same applies for all the roles:
class Player:
def __init__(self, name, sport):
self.name = name
self.sport = sport
class Goalkeeper(Player):
def __init__(self, name):
super().__init__(name, 'football')
class Defender(Player):
def __init__(self, name):
super().__init__(name, 'football')
class Midfielder(Player):
def __init__(self, name):
super().__init__(name, 'football')
class Striker(Player):
def __init__(self, name):
super().__init__(name, 'football')
Now, let’s create an object of type striker:
striker1 = Striker('James Striker')
print(striker1.__dict__)
As you can see, the namespace of the new object contains the attributes name and role:
{'name': 'James Striker', 'sport': 'football'}
The next step will be to add a new attribute to our classes.
Adding an Attribute to a Child Class
It’s time to add an attribute to our child classes. An attribute that only applies to football players and not necessarily to all sport players.
This is the power of inheritance. We can inherit functionality from a parent class and then provide additional functionalities that are only specific to child classes. It allows to avoid repeating code that is already in the parent class.
One attribute that is specific to football players but doesn’t apply to all players is the role.
For example, add the role to our Striker class:
class Striker(Player):
def __init__(self, name):
super().__init__(name, 'football')
self.role = 'striker'
We can now see the role attribute in the namespace of the instance of child class:
>>> striker1 = Striker('James Striker')
>>> print(striker1.__dict__)
{'name': 'James Striker', 'sport': 'football', 'role': 'striker'}
This code works but it’s not generic…
What if we want to create object of type Goalkeeper, Defender or Midfielder?
To make it generic we have to add the new attribute to the constructor of each child class.
So, for example, the Striker class becomes:
class Striker(Player):
def __init__(self, name, role):
super().__init__(name, 'football')
self.role = role
We need to remember to include the role when we create our striker object, otherwise we will receive the following error:
$ python football.py
Traceback (most recent call last):
File "football.py", line 28, in <module>
striker1 = Striker('James Striker')
TypeError: __init__() missing 1 required positional argument: 'role'
So, here is how we create the striker object now:
striker1 = Striker('James Striker', 'striker')
Quite cool! Our classes are slowly getting better.
Adding a Method to the Parent Class
And now…
…let’s add a method called play to our parent class:
class Player:
def __init__(self, name, sport):
self.name = name
self.sport = sport
def play(self):
pass
The method I have defined only includes the pass statement that, as we have seen before, in Python does nothing.
So, why are we adding it to the method?
Let’s create an object of type Player and the run the play method:
player1 = Player('Player1', 'football')
player1.play()
You will see that when you run this code you won’t get any output from the play method.
Let’s try to remove the pass statement from the method and see what happens when we execute the same code above:
$ python football.py
File "football.py", line 9
class Goalkeeper(Player):
^
IndentationError: expected an indented block
This time Python raises an indentation error caused by missing code inside the play method (that immediately precedes the definition of the class Goalkeeper.
So, we will add a print message to the play method of the parent class and move to the implementation of the same method for some of the child classes.
Here is how all our classes look like so far:
class Player:
def __init__(self, name, sport):
self.name = name
self.sport = sport
def play(self):
print("Player {} starts running".format(self.name))
class Goalkeeper(Player):
def __init__(self, name, role):
super().__init__(name, 'football')
self.role = role
class Defender(Player):
def __init__(self, name, role):
super().__init__(name, 'football')
self.role = role
class Midfielder(Player):
def __init__(self, name, role):
super().__init__(name, 'football')
self.role = role
class Striker(Player):
def __init__(self, name, role):
super().__init__(name, 'football')
self.role = role
Now, we can see how the play method is inherited by a child class. Let’s create an object of type Midfielder and execute the play method on it:
midfielder1 = Midfielder('James Midfielder', 'midfielder')
midfielder1.play()
The output is:
$ python football.py
Player James Midfielder starts running
When we invoke the play method on the Midfielder object the play method of the Player class is invoked. This is due to the Method Resolution Order.
The Method Resolution Order (MRO) is the order in which Python looks for a method within a hierarchy of classes.
You can use the mro() method of a class to see the resolution order:
print(Midfielder.mro())
[<class '__main__.Midfielder'>, <class '__main__.Player'>, <class 'object'>]
The output shows that the order used by Python to resolve methods in this case is:
- Midfielder class.
- Player class.
- object class that is the class most classes in Python inherit from.
So, in our scenario Python doesn’t find the play method in the Midfielder class and uses the same method from the parent class Player.
Override a Method in a Python Class
Overriding a method means defining a method in the child class with the same name of one of the methods in the parent class.
In this case, we can define the play method in the Midfielder class as follows:
class Midfielder(Player):
def __init__(self, name, role):
super().__init__(name, 'football')
self.role = role
def play(self):
print("Player {} passes the ball to a striker".format(self.name))
This time the print statement is more specific, it says that the midfielder passes the ball to a striker instead of printing a generic message that applies to all types of players.
Let’s execute this method on an object of type Midfielder in the same way we have done in the previous section:
midfielder1 = Midfielder('James Midfielder', 'midfielder')
midfielder1.play()
The output is:
$ python football.py
Player James Midfielder passes the ball to a striker
This time Python executes the method of the Midfielder child class because it’s implemented in it and it doesn’t execute the same method of the parent class (following the Method Resolution Order).
Calling a Parent Method From a Child Class
We have seen how the child class Midfielder was automatically resolving the play method from the parent class when it didn’t have an implementation for it.
But, are there scenarios in which we might want to explicitly call a parent method from a child class even if the same method exists in the child class?
Let’s find out!
I want to change the code so that when I execute the play method in one of the child classes two messages are printed:
- The first message says that the player starts running.
- The second message describes the next action that our player takes.
And in doing this we want to use the fact that the first message is already printed by the play method of the parent class and we want to avoid repeating it in the child classes:
For example, let’s update the play method of the Midfielder class:
class Midfielder(Player):
def __init__(self, name, role):
super().__init__(name, 'football')
self.role = role
def play(self):
super().play()
print("Player {} passes the ball to a striker".format(self.name))
Firstly, in the play method we use super() to call the play method of the parent class. And then we execute a print statement to show the second action taken by our midfielder.
And here’s what we see when we run the play method on an object of type Midfielder:
$ python football.py
Player James Midfielder starts running
Player James Midfielder passes the ball to a striker
In this example I’m using Python 3.
$ python --version
Python 3.7.4
I’m wondering if this also works with Python 2…
$ python2 --version
Python 2.7.14
$ python2 football.py
Traceback (most recent call last):
File "football.py", line 39, in <module>
midfielder1 = Midfielder('James Midfielder', 'midfielder')
File "football.py", line 25, in __init__
super().__init__(name, 'football')
TypeError: super() takes at least 1 argument (0 given)
We can see an error when calling super() without arguments if we use Python 2.
That’s because…
In Python 2 the super() method requires additional arguments compared to Python 3. We also need to explicitly inherit our parent class from object as shown below.
class Player(object):
def __init__(self, name, sport):
self.name = name
self.sport = sport
def play(self):
print("Player {} starts running".format(self.name))
...
...
...
class Midfielder(Player):
def __init__(self, name, role):
super(Midfielder, self).__init__(name, 'football')
self.role = role
def play(self):
super(Midfielder, self).play()
print("Player {} passes the ball to a striker".format(self.name))
I will explain the exact rationale behind this in another article about the difference between old style and new style classes in Python.
For now notice the following changes…
The definition of the parent class now starts with:
class Player(object):
And the two calls to super take two arguments: the subclass in which super() is called and the instance of the subclass:
super(Midfielder, self).__init__(name, 'football')
super(Midfielder, self).play()
In the next sections of this tutorial we will continue using the Python 3 syntax to call the super method.
Difference Between isinstance and issubclass With Python Classes
Let’s deepen our knowledge about Python classes in relation to inheritance.
In this last section we will look at the difference between the isinstance and issubclass Python built-in functions.
The difference between these two functions is explained in their name:
- isinstance applies to instances. It allows to check the type of a class instance (or object).
- issubclass applies to classes. It provides details about inheritance between classes.
Let’s start with isinstance…
The isinstance function takes two arguments in the following order: object and classinfo. It returns True if the object is an instance of classinfo or a subclass of it. Otherwise it returns False.
Here is what it returns when we apply it to our midfielder1 object defined in the previous section:
>>> print(isinstance(midfielder1, Midfielder))
True
>>> print(isinstance(midfielder1, Player))
True
As you can see the function returns True in both cases because midfielder1 is an instance of type Midfielder but also of type Player due to inheritance.
And now let’s look at issubclass…
The issubclass function takes two arguments: class and classinfo. It returns True if the class is a subclass of classinfo. Otherwise it returns False.
We will apply it to the Midfielder and Player classes:
>>> print(issubclass(Midfielder, Midfielder))
True
>>> print(issubclass(Midfielder, Player))
True
We already knew that Midfielder is a subclass of Player. But with the code above we have also learned that Midfielder is a subclass of Midfielder.
A class is a subclass of itself.
All clear?
Conclusion
We went through quite a lot in this article…
You have learned the:
- Basics about inheritance in Python.
- Difference between Parent and Child classes.
- Way to define methods in Child classes that override the same methods from the Parent classes.
- Technique to call parent methods from child classes.
- Difference between the Python built-in methods isinstance and issubclass.
And you? How are you using inheritance in your Python programs?
Let me know in the comments!
Also to deepen your knowledge about Python classes have a look at this article about Python abstract classes.
Related Course: This course offered by the University of Michigan introduces classes, instances, and inheritance.
Claudio Sabato is an IT expert with over 15 years of professional experience in Python programming, Linux Systems Administration, Bash programming, and IT Systems Design. He is a professional certified by the Linux Professional Institute.
With a Master’s degree in Computer Science, he has a strong foundation in Software Engineering and a passion for robotics with Raspberry Pi.