A class’s constructor is a special function that is invoked at object creation time. Constructors in Python are typically used to populate the class’s data members with default values. It is impossible for it to give any result other than null.
Constructors are used primarily to instantiate objects. Constructors are methods of a class that are invoked when an object of that class is formed in order to populate (assign values to) its data members. When a Python object is created, it calls the __init__() method, also known as the constructor.
Let’s have a look at the constructor’s usage.
Syntax of Python Constructor
def __init__(self):
# initializations
Init is one of the reserved functions in Python. In object-oriented programming, it is known as a constructor.
Rules of Python Constructor
Like all Python functions, it begins with the def keyword.
In its place comes the term “init,” which is surrounded by a pair of double-underscore brackets (__init__()).
It accepts a keyword parameter named self, which, when used to assign values to variables, is a pointer to the active instance of the class. When the constructor is used, it is created and passed on to __init__() either explicitly or indirectly.
Types of Constructors in Python
Parameterized Constructor
Non-Parameterized Constructor
Default Constructor
1. Parameterized Constructor in Python
When the constructor accepts arguments along with itself, it is known as a parameterized constructor.
These arguments can be used inside the class to assign values to the data members. Let’s see an example:
Code:
class Family:
# Constructor – parameterized
members=5
def __init__(self, count):
print(“This is parametrized constructor”)
self.members = count
def show(self):
print(“No. of members is”, self.members)
object = Family(10)
object.show()
Output:
This is parameterized constructor
No. of members is 10
Explanation
A new instance of the Family class is made. It contains a member variable.
A parameter (in this case, the number 10) is supplied during the object’s creation.
The constructor will use this value (10 in the preceding example) while creating the object.
The variable count is set to 10, and the self-members are also given the value 10.
The class’s self-members can be utilized for internal data printing.
2. Non-Parameterized Constructor in Python
When the constructor doesn’t accept any arguments from the object and has only one argument, self, in the constructor, it is known as a non-parameterized constructor.
This can be used to re-assign a value inside the constructor. Let’s see an example:
Code:
class Fruits:
favourite = “Apple”
# non-parameterized constructor
def __init__(self):
self.favourite = “Orange”
# a method
def show(self):
print(self.favourite)
# Creating an object of the class
obj = Fruits()
# calling the instance method using the object obj
obj.show()
Output:
Orange
Explanation:
In the preceding code snippet, our favorite fruit is set as an internal initial value of “orange” in the class. However, we’d like to make the modification right after the object is created.
This can be achieved by reassigning the value to the constructor. No arguments other than self are accepted by this kind of constructor. It can be used to see if our value has changed and to print it out.
3. Default Constructor in Python
When you do not write the constructor in the class you create, Python itself creates a constructor during the compilation of the program.
It generates an empty constructor that has no code in it. Let’s see an example:
Example
class Assignments:
check= “not done”
# a method
def is_done(self):
print(self.check)
# Creating an object of the class
obj = Assignments()
# calling the instance method using the object obj
.is_done()
Output
not done
Explanation
In the given scenario, we are determining if our homework is complete. We instantiate a Class object without providing a custom constructor.
If we do not supply a constructor to initialize the instance variables, Python will create one for us. It has no effect.
Conclusion
When a new instance of a class object is created, a method known as the constructor is invoked.
The generation of the constructors in python is dependent on the programmer; if the programmer does not create them, Python will build the default constructors automatically.