Menu Close

Constructor in Python

In this guide, we are going to learn all about Constructor in Python. Constructors play the most important role in object-oriented programming. In this guide, we will see all about constructor in Python along with examples.

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 see understand constructor in Python along with examples.

Constructor in Python

Constructor in Python is a special method that is used to initializing an object. Python constructor is used to initializing to assign the value to data members of the class when the object of the class created.

The constructor is a special method that is called automatically when the instance of the class is created.

Creating constructor in Python

To create the constructor in Python, use __init__() special method. This method is called When the class is instantiated.__init__() method take self as 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 type of constructor available in Python:

  • Default constructor
  • Parameterized Constructor

Default Conctsructor

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, Constructor with a parameter is known as 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.

Consclusion

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 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 special method that call automatically when the instance of the class is created.

What is __init__() in Python?

Ans:- __init__() method is 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 constructor.
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 refer to the current instance of the class. self in Python is used to access the attributes and method of the class.

For more information:- Click Here

Happy Coding
~ Vishvajit Rao

Python args and kwargs
Destructor in Python

Related Posts