
Overview
Python inheritance allows a class (child class) to inherit all the methods and properties from another class (parent class).
Creating a Parent Class
Any class can become a parent class, so the syntax is the same as creating any other class.
class Person:
def __init__(self, fname, lname):
self.firstname = fname
self.lastname = lname
def printname(self):
print(self.firstname, self.lastname)
x = Person("Johnny", "Shay")
x.printname()
Output:
Johnny Shay
Creating a Child Class
The child class can have new methods and properties, but will also inherit those of the parent class.
class Person: #parent class
def __init__(self, fname, lname):
self.firstname = fname
self.lastname = lname
def printname(self):
print(self.firstname, self.lastname)
class Student(Person): #child class
pass #because the class is created empty except for the inheritance
x = Student("Johnny", "Shay")
x.printname()
Note
In the example above, no additional methods and properties are added to the new class (child), so the pass statement is used to allow for the creation of a seemingly empty class (it will get the parent’s methods and properties) without throwing an error.
Output:
Johnny Shay
Using the __init()__ Function
When you add the __init()__ function, the child class will no longer inherit the parent’s __init()__ function.
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)
x = Student("Johnny", "Shay")
x.printname()
Output:
Johnny Shay
Using the super() Function
The super() function makes the child class inherit all the methods and properties even without specifying the name of the parent element.
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)
x = Student("Johnny", "Shay")
x.printname()
Output:
Johnny Shay
Adding Properties to the Child Class
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)
self.firstyear = 2015 #property added
x = Student("Johnny", "Shay")
print(x.firstyear)
Output:
2015
Passing Properties to the Child Class
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.firstyear = year #property will be passed
x = Student("Johnny", "Shay", 2015)
print(x.firstyear)
Output:
2015
Adding Methods to the Child Class
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): #method added
print("Welcome", self.firstname, self.lastname, "to the freshman class of", self.graduationyear)
x = Student("Johnny", "Shay", 2025)
x.welcome()
Note
If you add a method in the child class with the same name as a function in the parent class, the inheritance of the parent method will be overridden.
Output:
Welcome Johnny Shay to the freshman class of 2025
Python Notes:
- The most recent major version of Python is Python 3; however, Python 2 is still in use and quite popular, although not being updated with anything other than security updates
- Python uses new lines to complete a command, as opposed to other programming languages which often use semicolons or parentheses
- Python relies on indentation, using whitespace to define scope, such as the scope of loops, functions, and classes; other programming languages often use curly-brackets for this purpose
- Python string methods return new values, and DO NOT change the original string
- Python tuples are unchangeable after created (their items CANNOT be changed or re-ordered at a later point)
- Python sets are unordered (may appear in random orders when called), unchangeable (the value of individual items cannot be changed after creation), unindexed (items cannot be referred to by index or key), and duplicates are NOT ALLOWED
- As of v3.7, Python dictionaries are ordered and duplicates ARE ALLOWED; in v3.6 and earlier, dictionaries were unordered (did not have a defined order and could not be referred to using an index)
- Python does not have built-in support for arrays, but Python lists can be used as pseudo “arrays”; therefore, all Python list methods will work with these pseudo “arrays”
We’d like to acknowledge that we learned a great deal of our coding from W3Schools and TutorialsPoint, borrowing heavily from their teaching process and excellent code examples. We highly recommend both sites to deepen your experience, and further your coding journey. We’re just hitting the basics here at 1SMARTchicken.