In this guide, we are going to learn all about Constructor in Python. Constructors play the most important role in object-oriented programming. Constructor in Python is called automatically when the object of the class is being created.
In previous tutorials, We have seen concepts of Oriented Programming along with examples. To understand this article, you should have basic knowledge of Python programming.
Let’s understand Python constructor along with examples.
Headings of Contents
Constructor in Python
Constructor in Python is a special method that is used to initialize an object. Python constructor is used to assign the value to data members of the class when the object of the class is created otherwise we can perform some operations that are necessary during the creation of the object.
We don’t need to call the Python constructor manually when it will execute automatically when the object of the class is being created.
Creating a constructor in Python
To create the constructor in Python, use __init__() special method. This method is called When the class is instantiated.__init__() method takes self as the first argument that is used to access the attributes and methods of the class.
In Python,__init__() is called a constructor, It is always called when the object of the class is created.
Syntax:
The syntax of the class is as follows.
def __init__(self):
#body of the constructor.
Types of constructor in Python:
There are two types of constructors available in Python:
- Default constructor
- Parameterized Constructor
Default Constructor
A default constructor is a simple constructor that does not accept any arguments. It has only one argument that is self. self refers to the instance of the class.
Example:
class Person:
#default constructor
def __init__(self):
self.name = "Welcome to Programming Funda"
#method for printing the value of name attribute.
def welcome(self):
print(self.name)
#creating the instance of the class
p1 = Person()
#calling the instance method using object.
p1.welcome()
Output will be:- Welcome to Programming Funda
Parameterized constructor
In Python, a Constructor with a parameter is known as a parameterized constructor.
The parameter constructor takes self as the first argument that refers to the instance of the class and the rest of the argument provided by the programmer.
Example:
class Person:
#parameterized constructor
def __init__(self, name, age):
self.name = name
self.age = age
#method for printing the details of the person.
def Details(self):
print(f"My name is {self.name} and I am {self.age} years old.")
#creating the instance of the class
p1 = Person('Vishvajit', 22)
#calling the instance method using the object.
p1.Details()
Output will be:- My name is Vishvajit and I am 22 years old.
Conclusion
In this guide, you have learned all about Constructor in Python along with examples.
There are two types of constructors available in Python first is non parameterized constructor and the second is the parameterized constructor.
If you working with object Object object-oriented programming, Then you can’t ignore the constructor in Python.
If you like this article, please share and keep visiting for further Python tutorials.
Python OOPs Tutorials
FAQs about Python constructor
What is Constructor in Python?
Ans:- Constructor in Python is a special method that calls automatically when the instance of the class is created.
What is __init__() in Python?
Ans:- __init__() method is a reversed method in Python class that is also called Python constructor. It is always called when the class is instantiated.
How many constructors are there in Python?
Ans:- In Python, There are two types of constructors.
1. Default constructor
2. Parameterized constructor
How do you create a constructor in Python?
Ans:- To create a constructor in Python, you have to use __init__() method.
Why self is used in Python?
Ans:- In Python, self refers to the current instance of the class. self in Python is used to access the attributes and methods of the class.
For more information:- Click Here
Happy Coding