Hello everybody,
Michael here, and today’s lesson will be on inheritance in Python. Inheritance in Python is the same concept as inheritance in Java-a child class inherits all of the methods and attributes from another class called the parent class. However, inheritance in Python is executed differently than inheritance in Java, and this post will describe how inheritance in Python works.
To demonstrate inheritance, let’s first create a parent class:

In the parent class Team, I have two functions-__init__ and printInfo. The __init__ function accesses the class’s two main parameters-teamName and Location. The printInfo function prints the String The {teamName} are located in {location}.
- The f in the
printline denote that the String I’m trying to print is an f-string literal, which is a neat way to format Strings and concatenate values. - When inputting the parameter names into the f-string literal, remember to use the keyword
self(or whatever word you used in place ofself) in front of the parameter name just as I did forself.teamNameandself.location.
OK, so now let’s create an object of the Team class and execute the printInfo method:
Now let’s create a class called BasketballTeam, which will serve as a child class of the Team class:
Inheritance with Python classes is conceptually the same as inheritance with Java classes but inheritance with Python classes has much different syntax.
To create a child class in Python, use this general syntax: class ChildClass (ParentClass).
You’ll also notice that I used the pass keyword as the body of the BasketballTeam class. However, when it comes to child classes, pass doesn’t mean the class is empty. Instead, pass means that all methods and attributes from the parent class are inherited, but you don’t want to add any new methods or attributes to the child class that aren’t in the parent class.
Now let’s create an object of the BasketballTeam class and call the printInfo function:
Looks like the inheritance worked like a charm!
Now, what if we wanted to use the __init__ function for our child class instead of the pass keyword? Let’s see how we would execute that:
In this example, the child class’s __init__ function overrides the parent class’s __init__ function. However, the printInfo function is unaffected by this change.
To keep the parent class’s __init__ function, be sure to call the parent class’s __init__ function in the child class’s __init__ function:
- As you can see, the
printInfofunction works the same regardless of whether I use the parent class’s__init__function or the child class’s__init__function.
Now, what if you wanted the child class to inherit all of the methods and attributes from the parent class? To do this, use the keyword super (with parentheses):
When using the super() keyword in a child class, keep these three things in mind:
- Place the
super()keyword in the body of the child class’s__init__function. - After using the
super()keyword, remember to call the__init__function. Also remember to use all of the parameters of the child class’s__init__function except forself(or whatever you decided to call theselfparameter). Super()inherits all of the methods and attributes from the parent class, so there’s no need to mention the parent class (Teamin this case)
Now, what if we wanted to add an attribute that is exclusive to the child class? Let’s see how we can accomplish that:
To add an attribute that is exclusive to a child class, use this syntax: self.(name of attribute) = value of attribute. Also remember to place any new attributes in the body of the child class’s __init__ function (not the super().__init__ function call).
Now I need to make another change to my code? What could that be?:
I needed to include my new attribute yearFounded as a parameter in the child class’s __init__ function. Whenever you create a new attribute that is exclusive to the child class, you SHOULD include it as a parameter in the child class’s __init__ function. By doing so, any new attribute you created can be used as a parameter when creating an object of the child class.
- You don’t absolutely need to include the new attribute(s) in the parameters of the child class’s
__init__function but if you don’t include them, you won’t be able to use the new attribute(s) when creating an object of the child class.
OK, but what if you also wanted to add a function exclusive to the child class? Let’s see how we can do that:
To add a function exclusive to the child class, simply create a new function in the body of the child class with self (or whatever you want to use in place of self) as the sole parameter. DO NOT place the new function in the body of the child class’s __init__ function!
As you can see here, the new function I added-teamDescription works perfectly with the child class BasketballTeam. However, watch what happens when I try to use the teamDescription function with an object of the parent class Team:
I can’t use the teamDescription function on the parent class Team because the teamDescription function is exclusive to the child class BasketballTeam; therefore, it can only be used by the child class.
Thanks for reading,
Michael