Constructor and Destructor in Python

1128

Constructor in Python

Constructor, as the name, suggests that is constructing something. or, in other words, we can say it is the first step of any task.

Constructors are generally used for initializing an object. In other languages, we need to make a method with the same name as the class, to declare a constructor. But in Python, the __init__() method is called the constructor and is always called when an object of the class is created.

Syntax of declaration of constructor in Python

def __init__(self):

Types of constructor in Python

Basically, there are two types of constructors in Python –

  1. Default Constructor
  2. Parameterized Constructor

Default Constructor

The default constructor is a constructor that is automatically invoked even if the programmer doesn’t make any constructors inside the class it creates. Its definition has only one argument which is the reference to the instance being constructed.

We will understand this with the help of the example given below –

class CrazyCoder:
    def __init__(self):
        self.code = "Yuvayana Tech"
    def print_code(self):
        print(self.code)
obj = CrazyCoder()
obj.print_code()

Output –

Yuvayana Tech

Parameterized Constructor in Python

A constructor with parameters passed as arguments is called a parameterized constructor. The parameterized constructor takes its first argument as a reference to the instance being constructed known as self and the rest of the arguments are provided by the programmer.

We will understand this better with the help of the example given below –

class Addition:
    num1 = 0
    num2 = 0
    answer = 0
    def __init__(self, f, s):
        self.num1 = f
        self.num2 = s
    def display(self):
        print("Num1 number = " + str(self.num1))
        print("Num2 number = " + str(self.num2))
        print("Addition of two numbers = " + str(self.answer))
    def calculate(self):
        self.answer = self.num1 + self.num2
obj = Addition(500, 600)
obj.calculate()
obj.display()

Output –

Num1 number = 500
Num2 number = 600
Addition of two numbers = 1100

Destructor in Python

Destructors are the last code that runs when the object of a class gets destroyed. In python, destructors are not much needed as in C++ and Java. Because Python by-default provides the garbage collector which handles the memory management.

Like C++ and Java, we don’t need to create a function with the same name. But here, we have a  __del__(self) method to do this.

Syntax for declaration of destructor in python

def __del__(self):
class Animals:
    def __init__(self):
        print('The class called Animals is CREATED.')
    def __del__(self):
        print('The destructor is called for deleting the Animals.')
object = Animals()
del object

Output –

 The class called Animals is CREATED.

The destructor is called for deleting the Animals.

So, in this article, we learned about the concepts of constructors used to initialize objects and destructors which are used for memory management.

Attempt Free Python Online Test

Previous articleInheritance in Python
Next articleException Handling in Python (Try, Except, Else, Finally)
Harshit Brijwasi
I am Harshit Brijwasi. I am an engineer and a Technical Content writer. I am here to share my knowledge about technical topics and help researchers with them.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.