This lesson will teach you about Constructors, Types of constructors with examples in Python, and how to implement destructors in your code.
Constructors in Python are accountable for initializing (assigning values) the Class’s data members when an object is created. Typically, constructors are used to instantiate an object. In Python, the __init__() method is called by the constructor when an object is formed. It also gets called when the object is defined.
Syntax of constructor declaration
def __init__(self): # body of the constructor
There are two types of constructors:
The default constructor is a simple constructor that does not accept any parameters, and the single argument to its definition references the instance being produced.
Let’s see an illustration of a default constructor below.
#!/usr/bin/python # -*- coding: utf-8 -*- class DefaultConstructor: # default constructor def __init__(self): self.defConst = 'Default Constructor' # a method for printing data members def print_defConst(self): print self.defConst # creating an object of the class obj = DefaultConstructor() # calling the instance method using the object obj obj.print_defConst()
The term parameterized constructor refers to a constructor that accepts parameters. The parameterized constructor receives as its first argument a reference to the instance being produced, referred to as self. At the same time, the remaining parameters are supplied by the programmer.
Let’s see an example of Parameterized constructor below.
#!/usr/bin/python # -*- coding: utf-8 -*- class AddCalculator: Var1 = 0 Var2 = 0 Result = 0 # parameterized constructor def __init__(self, f, s): self.Var1 = f self.Var2 = s def display(self): print 'First variable = ' + str(self.Var1) print 'Second variable = ' + str(self.Var2) print 'AddCalculator of two numbers = ' + str(self.Result) def calculate(self): self.Result = self.Var1 + self.Var2 # creating object of the class # this will invoke the parameterized constructor obj = AddCalculator(200, 3) # perform AddCalculator obj.calculate() # display result obj.display()
First variable = 200 Second variable = 3 AddCalculator of two numbers = 203
After an object is destroyed, destructors are invoked. Python does not require as many destructors as C++ because its garbage collector handles memory management automatically.
In Python, the __del__() method is a destructor method. It is invoked after all references to the object have been removed, i.e. when an object’s references have been garbage collected.
Syntax of destructor declaration
# body of destructor
A reference to an object is also removed when the object is no longer referenced or when the application terminates.
Here is a simple illustration of a destructor. Using the del keyword, we erased all references to the object ‘obj’; therefore, its destructor was immediately called.
#!/usr/bin/python # -*- coding: utf-8 -*- # Python program to illustrate destructor class Student: # Initializing def __init__(self): print 'Student created.' # Deleting (Calling destructor) def __del__(self): print 'Destructor called, Student deleted.' obj = Student() del obj
Student created. Destructor called, Student deleted.