Classes and objects in python
Python is a object oriented programming language. An object oriented consists of classes and objects. An object is the instance of a class. Objects have associated variables and functions with a class. In the following program ‘Game’ is a class and ‘x1’ is an object for the class ‘Game’.
class Game:
Player = "Football"
x1 = Game()
print(x1.Player)
Output :
Football
A class has built in function __init__(), which is also called method. This function is run first as soon as an object is created. This function is used to assign values for the object properties and also to carry out other operations when required for the object created.In the following program ‘Game’ is the name of the class. __init__ is the built in function. This function has values for the object properties. Here p1 and p2 are the objects for the class ‘Game’. Functions defined inside the class are called methods. Here ‘function_name’ is a method.
class Game:
def __init__(self, name, player):
self.name = name
self.player = player
def function_name(self):
print("The name of the sportsman is " + self.name)
print("The player is playing the game "+ self.player)
p1 = Game("XYZ", "Football")
p2 = Game ("PQR","Cricket")
p1.function_name()
p2.function_name()
Output :
The name of the sportsman is XYZ
The player is playing the game Football
The name of the sportsman is PQR
The player is playing the game Cricket
The first parameter of the function is the reference parameter for the current instance of the class. It is used to access the variables which belongs to that class. Here the reference parameter is the ‘self’. It can be of any name.
Inheritance and multiple inheritance
Inheritance is the feature which allows to inherit properties and methods from another class. The class from which these are inherited is called a base class or parent class. The class into which properties and methods are inherited from base class is called derived or child class. In the following program class ‘Team’ is the child class of class ‘Game’. Class ‘Game is the parent class’.
class Game:
def __init__(self, name, player):
self.name = name
self.player = player
def function_name(self):
print("The name of the sportsman is " + self.name)
print("The player is playing the game "+ self.player)
p1 = Game("XYZ", "Football")
p2 = Game ("PQR","Cricket")
p1.function_name()
p2.function_name()
class Team (Game):
pass
In the following program child class ‘Team’ has executed the method or function, ‘function_name’ of parent class ‘Game’ by creating an object p3.
class Game:
def __init__(self, name, player):
self.name = name
self.player = player
def function_name(self):
print("The name of the sportsman is " + self.name)
print("The player is playing the game "+ self.player)
p1 = Game("XYZ", "Football")
p2 = Game ("PQR","Cricket")
p1.function_name()
p2.function_name()
class Team (Game):
pass
p3 = Team ("ABC","Volleyball")
p3.function_name()
Output :
The name of the sportsman is XYZ
The player is playing the game Football
The name of the sportsman is PQR
The player is playing the game Cricket
The name of the sportsman is ABC
The player is playing the game Volleyball
On adding the __init__ function, it is called automatically when an object is created. This function in the child class will override the parent’s __init__ function, so it will no longer get inherited. In order to inherit from parent’s class it should be called using name of the class or by using super function,’super()’. Super function is used to inherit all the methods and properties from the parent class.
In the following example child class ‘Team’ inherits all properties and methods from the base class ‘Game’. Here, the object p3 inherits’ name’ and ‘player’ from parent ‘Game’ class.
class Game:
def __init__(self, name, player):
self.name = name
self.player = player
def function_name(self):
print("The name of the sportsman is " + self.name)
print("The player is playing the game "+ self.player)
class Team (Game):
def __init__(self,name,player, totalplayer,country):
super().__init__(name,player)
self.totalplayer = totalplayer
self.country = country
def function_name2(self):
print("Total number of player in the team is " + self.totalplayer)
print("The team is for the country " + self.country)
p3 = Team ("ABC","Volleyball","11","C")
p3.function_name()
p3.function_name2()
Output :
The name of the sportsman is ABC
The player is playing the game Volleyball
Total number of player in the team is 11
The team is for the country C
The syntax of multiple inheritance is similar to the single inheritance. In multiple inheritance a child class is derived from multiple parent class or the base class. In the following program class ‘coach’ is derived from two class ‘Game’ and ‘Team’.
class Game:
def __init__(self, name, player):
self.name = name
self.player = player
def function_name(self):
print("The name of the sportsman is " + self.name)
print("The player is playing the game "+ self.player)
class Team:
def __init__(self,totalplayer,country):
self.totalplayer = totalplayer
self.country = country
def function_name2(self):
print("Total number of player in the team is " + self.totalplayer)
print("The team is for the country " + self.country)
class Coach (Game,Team):
def __init__(self,name,player,totalplayer,country,coachname):
Game.__init__(self,name, player)
Team.__init__(self,totalplayer, country)
self.coachname = coachname
def function_name3(self):
print("The name of the coach is " + self.coachname)
p4 = Coach ("ABC","Football","11","X","PQR")
p4.function_name()
p4.function_name2()
p4.function_name3()
Output:
The name of the sportsman is ABC
The player is playing the game Football
Total number of player in the team is 11
The team is for the country X
The name of the coach is PQR
In multilevel inheritance a new derived class is inherited from a derived class which is inherited from a base class. It can go to any level of depth to the derivation of the derived class. In the following program class ‘Team’ is derived from class ‘Game’ and class ‘Coach’ is derived from derived class ‘Team’.
class Game:
def __init__(self, name, player):
self.name = name
self.player = player
def function_name(self):
print("The name of the sportsman is " + self.name)
print("The player is playing the game " + self.player)
class Team (Game):
def __init__(self, name, player, totalplayer, country):
Game.__init__(self, name, player)
self.totalplayer = totalplayer
self.country = country
def function_name2(self):
print("Total number of player in the team is " + self.totalplayer)
print("The team is for the country " + self.country)
class Coach (Team):
def __init__(self,name,player,totalplayer,country,coachname):
Team.__init__(self, name, player, totalplayer, country)
self.coachname = coachname
def function_name3(self):
print("The name of the coach is " + self.coachname)
p4 = Coach ("ABC","Football","11","X","PQR")
p4.function_name()
p4.function_name2()
p4.function_name3()
Output :
The name of the sportsman is ABC
The player is playing the game Football
Total number of player in the team is 11
The team is for the country X
The name of the coach is PQR