Python Class Inheritance

In OOP, class inheritance refers to a child class that inherits attributes from a parent class. Programmers use this method as a code reuse methodology to extend the functionality of classes. If you are developing a class that has the basic attributes and methods as another class and you want to add one or more specific functions, then you can use class inheritance.

Using the grade book application as an example, we have a shared attribute between students and instructors. We know that both groups have names, ages, and courses, but students have added numerical grades. If we develop a basic class called ‘person’ with the shared attributes and derive 2 classes called ‘student’ and ‘instructor,’ then we can extend each one individually. As a quick example, check out this code snippet.

class person:
    def __init__(self,name,age): = name
        self.age = age = []

class instructor(person):
    def __init__(self,name,age):
        super().__init__(name, age)
        self.studentsAssigned = 0

    def getDetails(self):
        print("{} has {} students assigned".format(, str(self.studentsAssigned)))

class student(person):
    def __init__(self,name,age):
        super().__init__(name, age)
        self.grades = []
    def getDetails(self):
        print("{} has the following grades: {}".format(, str(self.grades)))     

student1 = student(age=20,name="John Doe")
instructor1 = instructor(age=30, name="Obi Wan")

instructor1.studentsAssigned = 19

It is important to note that the child __init__ class will override the default variables of the parent class. To adapt the variables from the parent class, we use a function called super. Super tells the child class to include the parent class variables along with any new ones created. If you do not include super, then name and age would not exist in the child class.

Additional Resources

More on Class Inheritance

1 thought on “Python Class Inheritance”

Leave a Comment