Multiple Inheritance in Python


Last Blog | Index | Next Blog

Books | Project Ninety | Python | Training


28 January 2022

Basic inheritance works like this: let's say you have a Car class and you want to create a new class that inherits from that called BondCar and let's say the Car has an initialization method and a drive method.

            class Car():
                def __init__(self):
                    print("This is a car.")

                def drive(self):
                    print("Vroom! Vroom!")

            class BondCar(Car):
                def __init__(self):
                    print("This is a Bond car.")
          

So when you instantiate a BondCar then you can call the the drive method and it will call the drive method of the parent class Car.

            AstonMartin = BondCar()
            AstonMartin.drive()
          
This is a Bond car.
Vroom! Vroom!

Of course, being that you're James Bond you always need to up your game (or maybe you're Q maybe you're the one building the things for James Bond). So you want the BondCar class to inherit from not only Car but also from another class, Submarine, which also has a drive method.

            class Submarine():
                def __init__(self):
                    print("This is a submarine.")

                def drive(self):
                    print("Bubble, bubble...")
          

This leads to a conundrum. There's an init method in both Car and Submarine and there's a drive method in both Car and in Submarine. Thus, when I instantiate my BondCar that inherits from both, which parent class' init method is going to be called? And when I call the drive method which parent class' drive method will be called?

            class BondCar(Car, Submarine):
                def eject(self):
                    print("Ejecting villian!")
          

Python fortunately resolves this ambiguity by giving the first class (in this case Car) priority. When the drive method is called whichever class is first in the inheritance list is the one whose drive method will be called.

            AstonMartin = BondCar()
            AstonMartin.drive()
            AstonMartin.eject()
          
This is a car.
Vroom! Vroom!
Ejecting villian!

But suppose you want to access the drive method from the second Submarine class instead. Then you need to use the parent class name and overwrite the method in the BondCar class:

            class BondCar(Car, Submarine):
                def __init__(self):
                    Car.__init__(self)
                    Submarine.__init__(self)

                def drive(self):
                    Submarine.drive(self)

                def eject(self):
                    print("Ejecting villian!")
          
When instantiated the BondCar then uses methods from both parents.
            AstonMartin = BondCar()
            AstonMartin.drive()
          
This is a car.
This is a submarine.
Bubble, bubble...


Last Blog | Index | Next Blog


Web bradley.wogsland.org

Last altered 29 January 2022 by Bradley James Wogsland.

Copyright © 2022 Bradley James Wogsland. All rights reserved.