Python Inheritance
With inheritance, one class can take on all the methods and properties of another class.
The class that gives them away is the parent class, sometimes called the base class.
The class that receives them is the child class, also known as the derived class.
Create a Parent Class
A parent class needs no special syntax. You write it like any other class, and any class can take this role:
Example
Person stores firstname and lastname properties and offers a printname method:
Output: complete Python program
class Person:
def __init__(self, fname, lname):
self.firstname = fname
self.lastname = lname
def printname(self):
print(self.firstname, self.lastname)
#Use the Person class to create an object, and then execute the printname method:
x = Person("John", "Doe")
x.printname()
Create a Child Class
To make a child class, write the parent's name in parentheses after the new class name, so the functionality carries over:
Note: pass is what you write when the class needs nothing of its own beyond what it inherits.
As a result, Student ends up with every property and method that Person has.
Example
Student takes its properties and methods from the Person class:
Output: complete Python program
class Person:
def __init__(self, fname, lname):
self.firstname = fname
self.lastname = lname
def printname(self):
print(self.firstname, self.lastname)
class Student(Person):
pass
x = Student("Mike", "Olsen")
x.printname()
class Student(Person):
pass
Add the __init__() Function
Our child class so far simply receives everything from its parent.
Next, replace the pass keyword with an __init__() function of its own.
Note: Python calls __init__() by itself whenever the class creates a new object.
There is a catch: a child class with its own __init__() stops inheriting the parent's version.
Note: The child's __init__() overrides the one it would have inherited from the parent.
To get the parent's behavior back, call the parent's __init__() from inside the child's:
The child now has its own __init__() and still keeps what the parent provides, so it is ready for you to add new functionality.
Example
Output: complete Python program
class Person:
def __init__(self, fname, lname):
self.firstname = fname
self.lastname = lname
def printname(self):
print(self.firstname, self.lastname)
class Student(Person):
def __init__(self, fname, lname):
Person.__init__(self, fname, lname)
class Student(Person):
def __init__(self, fname, lname):
#add properties etc.
Use the super() Function
The built-in super() function is another way to reach the parent from a child class:
Because super() finds the parent for you, the parent's name never has to appear in the call, and you still get its methods and properties.
Example
Output: complete Python program
class Person:
def __init__(self, fname, lname):
self.firstname = fname
self.lastname = lname
def printname(self):
print(self.firstname, self.lastname)
class Student(Person):
def __init__(self, fname, lname):
super().__init__(fname, lname)
Add Properties
A graduation year such as 2019 should not be written into the class. It should be a variable, given to Student each time a student object is created, and that takes one more __init__() parameter:
Example
Add a year parameter, and supply the right value as each object is created:
Output: complete Python program
class Person:
def __init__(self, fname, lname):
self.firstname = fname
self.lastname = lname
def printname(self):
print(self.firstname, self.lastname)
class Student(Person):
def __init__(self, fname, lname, year):
super().__init__(fname, lname)
self.graduationyear = year
x = Student("Mike", "Olsen", 2019)
Add Methods
Child classes can have methods of their own. One thing to watch: a child method that shares its name with a parent function overrides the inherited one.
Example
Give the Student class a welcome method:
Output: complete Python program
class Person:
def __init__(self, fname, lname):
self.firstname = fname
self.lastname = lname
def printname(self):
print(self.firstname, self.lastname)
class Student(Person):
def __init__(self, fname, lname, year):
super().__init__(fname, lname)
self.graduationyear = year
def welcome(self):
print("Welcome", self.firstname, self.lastname, "to the class of", self.graduationyear)