One of the main reasons for using Object Orientated coding is being able to reuse code in many different projects. For example, if we created a program to read configuration files, and the code was Object Orientated (OO), then we would be able to use this code in any other program that we would want to read a config. file.
Inheritance is another way to reuse code. We can create a class, and then use variables and functions from it in another class. Thus, this new class (the sub-class) inherits information from the parent class.
The example below was taken from A byte of Python (which is an excellent guide to programming in Python). This example shows how to use inheritance while programming a system for data management within a school. In this very simple piece of code we have each member of the school logging their Name and Age. We then have classes for both members of staff (where their salary is stored) and students (where an average grade is stored)
class schoolMember():
no_members = 0
def __init__(self,name,age):
self.name = name
self.age = age
schoolMember.no_members += 1
def details(self):
print "\tName: %s, age: %s"%(self.name,self.age)
def number(self):
print "Number of members: ",schoolMember.no_members
class student(schoolMember):
def __init__(self,name,age,average):
schoolMember.__init__(self,name,age)
print "Student:"
self.averageGrade = average
schoolMember.details(self)
print "\tAverage Grade: ", self.averageGrade
schoolMember.number(self)
class teacher(schoolMember):
def __init__(self,name,age,salary):
schoolMember.__init__(self,name,age)
print "Teacher:"
schoolMember.details(self)
self.salary = salary
print "\tSalary: %i"%self.salary
schoolMember.number(self)
a = student("Matt",26,78)
b = teacher("El",25,30000)
c = student("Dave",31,42)
From this code sample we can see that the first fourteen lines declare the parent-class which has variables to represent the users name and age, a function to print out details from the variables in the class and a function to print the number of members in the “school”. Also, whenever an instance of the class is created the no_members variable is incremented (see the next paragraph for an explanation of class variable types).
This is an interesting point, where we define variables as self. are instance variables. These variables are “local” to that instance of the class (see later). Any variables accessed using schoolMember. are class variables. This variable can be updated from any instance of the class. In this instance I use it to store the total number of instances of the class. Another example could be an array that can be updated by each instance of a class. This could be used with threading (using locks of course) to update a “global” array with data from many threads.
Lines 16 – 23 are used to declare a class storing student information. This class inherits from the schoolMember class using the class CLASS_NAME(OTHER_CLASS): syntax. We then call the initialisation function __init__ of the parent class with the student’s name and age. We can then set the “local” class average grade variable. We then call the function in the schoolMember class to display the information from that class.
Lines 25 – 32 do not require explanation as they do pretty much the same thing as the previous chunk of code.
The final three lines of code create instances of the student and teacher class and provide them with the information required.
The output of the code can be seen below:
Student:
Name: Matt, age: 26
Average Grade: 78
Number of members: 1
Teacher:
Name: El, age: 25
Salary: 30000
Number of members: 2
Student:
Name: Dave, age: 31
Average Grade: 42
Number of members: 3
Please ask any questions in the comments!


