Constructors

A constructor is a special type of method (function) which is used to initialize the instance members of the class.

Constructors can be of two types.

  1. Parameterized Constructor

  2. Non-parameterized Constructor

Constructor definition is executed when we create the object of this class. Constructors also verify that there are enough resources for the object to perform any start-up task.

Creating a Constructor

Example 1:

class Employee:  
    def __init__(self,name,id):  
        self.id = id;  
        self.name = name;  
    def display (self):  
        print("ID: %d \nName: %s"%(self.id,self.name))  
emp1 = Employee("John",101)  
emp2 = Employee("David",102)  
  
#accessing display() method to print employee 1 information  
   
emp1.display();   
  
#accessing display() method to print employee 2 information  
emp2.display();  

ID: 101

Name: John

ID: 102

Name: David

Example 2: Counting the number of objects of a class

Class Attribute (count = 0) will be updated to (count = 3) because constructor (Student.count) is called 3 times

Print(Student.count) will return 3 as Class Attribute is updated to count = 3

Print(s1.count) will return 3 as self.count is not present in constructor. So, s1.count will also pick value from class attribute (count=3). But if self.count would have been present here, then s1.count would have picked value of self.count

class Student:  
    count = 0  
    def __init__(self):  
        Student.count = Student.count + 1  
s1=Student()  
s2=Student()  
s3=Student()  
print("The number of students:",Student.count) 
print("The number of students:",s1.count)  

The number of students: 3

Example 3:

In this example, when instance attribute self.count is not assigned any value, it will automatically pick value from class attribute, count=5

Student.count will not be updated as self.count is called in constructor, self.count result will reflect when called via objects s1, s2.

class Student:  
    count = 5
    def __init__(self): 
        self.count = self.count + 1  
s1=Student()  
s2=Student()   
print("The number of students:",Student.count) 
print("The number of students:",s1.count)  
print("The number of students:",s2.count)  

The number of students: 5

The number of students: 6

The number of students: 6

Example 4:

In this example, when self.count is assigned a value self.count = 2, it will not pick value from class attribute (count = 15)

class Student:  
    count = 15
    def __init__(self): 
        self.count = 2
        self.count = self.count + 1  
s1=Student()  
s2=Student()   
print("The number of students:",Student.count)  
print("The number of students:",s1.count)  
print("The number of students:",s2.count) 

The number of students: 15

The number of students: 3

The number of students: 3

Example 5:

class Student:  
    count = 110  
    def __init__(self):  
        print(self.count)
        print(Student.count)
        Student.count = Student.count + 1  
        self.count = self.count + 1
p1 = Student()

print("The number of students:", p1.count)  
print("The number of students:", Student.count)

110

110

The number of students: 112

The number of students: 111

Example 6:

class Student:  
    count = "bird"
    def __init__(self):  
        self.count = "kuku" +self.count
s1=Student()  
s2=Student()  
s3=Student() 
print("The number of students:",s1.count)  

The number of students: kukubird

Example 7:

class Student:  
    count = "bird"
    def __init__(self):  
        Student.count = "kuku" +Student.count
s1=Student()  
s2=Student()  
s3=Student() 
print("The number of students:",s1.count)  

The number of students: kukukukukukubird

Non-Parameterized Constructor:

Example 8:

class Student:    
    # Constructor - non parameterized    
    def __init__(self):    
        print("This is non parametrized constructor")    
    def show(self,name):    
        print("Hello",name)    
student = Student()    
student.show("John")    

This is non parameterized constructor

Hello John

Parameterized Constructor:

class Student:    
    # Constructor - parameterized    
    def __init__(self, name):    
        print("This is parametrized constructor")    
        self.name = name    
    def show(self):    
        print("Hello",self.name)    
student = Student("John")    
student.show() 

This is parameterized constructor

Hello John

Last updated