Class Inheritance

Class inheritance is an important feature in object-oriented programming (OOP) that allows a new class (known as the “child” or “subclass”) to inherit attributes and methods from an existing class (known as the “parent” or “superclass”).

This provides several important benefits:

Class Inheritance in Python

Code
class Team():
    def __init__(self, city, name):
        self.city = city
        self.name = name

    def __repr__(self):
        return f"<Team '{self.name}'>"

    def __iter__(self):
        yield "city", self.city
        yield "name", self.name

    @property
    def full_name(self):
        return self.city + " " + self.name

    def advertise(self):
        print("COME TO", self.city.upper(), "TO SEE OUR GAMES!")

In this example, we are creating a BaseballTeam child class that inherits from the Team parent class, while adding in some baseball-specific functionality.

class BaseballTeam(Team):

    def __init__(self, city, name, short_stop=None, closing_pitcher=None):

        # ATTRIBUTES SHARED WITH PARENT:
        super().__init__(city=city, name=name)

        # CHILD-SPECIFIC ATTRIBUTES:
        self.short_stop = short_stop
        self.closing_pitcher = closing_pitcher

    # OVERRIDING PARENT METHOD:
    def advertise(self):
        print("COME TO", self.city.upper(), "TO SEE OUR BASEBALL GAMES, INCLUDING:")
        print("...", self.short_stop)
        print("...", self.closing_pitcher)

    # CHILD-SPECIFIC METHOD:
    def warm_up_bullpen(self):
        print("NOW WARMING UP:", self.closing_pitcher)

In this child class, in the initialization method, we invoke super(), which is a reference to the parent class, to initialize an instance of the parent class. Then we initialize some baseball specific parameters (in this case short_stop and closing_pitcher).

Here are some initialization and usage examples for the BaseballTeam child class:

bt = BaseballTeam(name="Yankees", city="New York",
                    short_stop="Derek Jeter",
                    closing_pitcher="Mariano Rivera"
)
print(type(bt))
print(bt)
<class '__main__.BaseballTeam'>
<Team 'Yankees'>

Inherited from the parent class:

print(bt.city)
print(bt.name)
print(bt.full_name)
New York
Yankees
New York Yankees

Overridden / customized in the child class:

bt.advertise()
COME TO NEW YORK TO SEE OUR BASEBALL GAMES, INCLUDING:
... Derek Jeter
... Mariano Rivera

Child-specific:

bt.warm_up_bullpen()
NOW WARMING UP: Mariano Rivera

Multiple Inheritance in Python

It is possible to use inheritance to “mix-in” functionality from multiple parent classes:

class Runner:
    def run(self):
        return "Runs on the track."

class Swimmer:
    def swim(self):
        return "Swims in the pool."

class Bicyclist:
    def bike(self):
        return "Cycles on the road."

# INHERITS FROM / "MIXES IN" MULTIPLE PARENTS:
class Triathlete(Runner, Swimmer, Bicyclist):
    pass
triathlete = Triathlete()

print(triathlete.run())
print(triathlete.swim())
print(triathlete.bike())
Runs on the track.
Swims in the pool.
Cycles on the road.