Python constructors - How to use them?
hrishikesh1990
Posted on April 26, 2022
In Python, in order to create and initialize an object of a class, you need a special method and that special method (function) is called constructors. Every class has a constructor, but it is not required to explicitly define it. In this tutorial, we will read about Python constructors and how to use them.
Table of contents
What are Constructors in Python?
Constructors are generally used for instantiating an object of a class. The main objective of the constructors is the assign values to the data members of a class when an object of the class is created.
Constructors are always called when an object is created and is simulated by the init() method. It accepts the self-keyword, which refers to itself (the object), as a first argument which allows accessing the attributes or method of the class.
Syntax:
def __init__(self):
Input:
class Student:
def __init__(self, name, roll_no)
self.name = name
self.roll_no = roll_no
def display(self):
print ("Roll No.: %d \nName: %s" % (self.roll_no, self.name))
# Creating object of the class
stud1 = Student("Alex", 34)
stud2 = Student("Mark", 67)
stud1.display()
stud2.display()
We can pass any number of arguments at the tie of creating the class object, based upon the init() definition. Remember, every class must have a constructor, even if it only relies on the default constructor.
Output:
Roll No.: 34
Name: Alex
Roll No.: 67
Name: Mark
In C++ or Java, the constructor has the same name as its class but in Python, it is not the same.
Types of Python constructors
There are two different types of constructors in Python:
Parameterized Constructor
The parameterized constructor has multiple parameters along with the self. It 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.
Input:
class Student:
# Parameterized constructor
def __init__(self, name, roll_no):
self.name = name
self.roll_no = roll_no
def display(self):
print ("Roll No.: %d \nName: %s" % (self.roll_no, self.name))
# Creating object of the class
stud1 = Student("Navya", 34)
stud2 = Student("Mia", 67)
stud1.display()
stud2.display()
In the above example, a parameterized constructor is defined which takes two parameters.
Output:
Roll No.: 34
Name: Navya
Roll No.: 67
Name: Mia
Non-parameterized Constructor
The non-parameterized constructor uses when we do not want to manipulate the value or the constructor that has only self as an argument. Its definition has only one argument which is a reference to the instance being constructed.
Input:
class Student:
# no-argument constructor
def __init__(self)
self.name = "Mark"
self.roll_no = "67"
def display(self):
print ('Roll No.:', self.roll_no, \n'Name:', self.name)
# Creating object of the class
stud = Student()
stud.display()
As you can see, there is no argument sent to a constructor while creating an object.
Output:
Roll No.: 67
Name: Mark
Closing thoughts
Remember, if you create four objects, the class constructor is called four times too. Just as Python has constructors, it also has destructors which are used to destroy the object of a class. One can learn about more Python concepts here.
Posted on April 26, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.