0% found this document useful (0 votes)
15 views

Constructors in Python: Definition, Types, and Rules: Vikram Singh

Very useful
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Constructors in Python: Definition, Types, and Rules: Vikram Singh

Very useful
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Constructors in Python: Definition, Types, and

Rules
Vikram Singh
Assist ant Manager - Cont ent
Updated on Jun 10, 2024 10:56 IST
A constructor in Python is a special method called when an object is created. Its
purpose is to assign values to the data members within the class when an object is
initialized. In this article, we will learn what a Python constructor is, its types, and the
rules for defining constructors in Python.

Constructors in Python is a special class method for creating and initializing an


object instance at that class. Every Python class has a constructor; it’s not required
to be defined explicitly. The purpose of the constructor is to construct an object and
assign a value to the object’s members.

Explore Python Courses

Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 11-Jun-20 24.
So, without further delay, let’s learn more about how to use constructors in Python.

Table of Content
What is Python Constructor

Types of Constructors
Rules of Python Constructor

Multiple Constructor in Python Class

What is Python Constructor


In Python, a constructor is a special method called when an object is created. Its
purpose is to assign values to the data members within the class when an object is
initialized. The name of the constructor method is always __init__.

Here is an example of a simple class with a constructor:

Copy code

class Person:
def __init__(self, name, age):
self.name = name
self.age = age

person = Person("John", 30)


print(person.name)
print(person.age)

Output:
John

Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 11-Jun-20 24.
30

In this example, the __init__ method is called when the Person object is created,
and it sets the name and age attributes of the object.

The __init__ method is commonly referred to as the “constructor” because it is


responsible for constructing the object. It is called automatically when the object is
created, and it is used to initialize the object’s attributes.

Convert YouTube Videos t o MP3 using Pyt hon


Pytho n is the ultimate to o l fo r all yo ur tech-savvy needs, and its ability to co nvert
Yo uTube video s to MP3 files is no exceptio n. If yo u’re lo o king fo r a way
to ...re ad m o re

Bubble Sort Algorit hm in Pyt hon


In this article, we will briefly discuss what bubble so rt algo rithms in pytho n is,
ho w to implement it in pytho n, its wo rking, and co mplexity.

Fibonacci Number
In mathematics, we have different types o f numbers like integers, natural, real,
ratio nal, etc. Alo ng with these, we have a beautiful number with a remarkable
pro perty kno wn as a Fibo nacci...re ad m o re

Types of Python Constructor


In Python, there are two types of constructors:

Def ault Constructor: A def ault constructor is a constructor that


takes no arguments. It is used to create an object with def ault values
f or its attributes.

Parameterized Constructor: A parameterized constructor is a


constructor that takes one or more arguments. It is used to create an

Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 11-Jun-20 24.
object with custom values f or its attributes.
Non-Parameterized Constructor: A non-parameterized constructor
is a constructor that does not take any arguments. It is a special
method in Python that is called when you create an instance of a
class. The non-parameterized constructor is used to initialize the
def ault values f or the instance variables of the object.

What is Programming What is Python


What is Data Science What is Machine Learning

Now let’s look into each one of them in detail.

Default Constructors
Default constructors are useful when you want to create an object with a
predefined set of attributes, but you don’t want to specify the values of those
attributes when the object is created.

Here is an example of a default constructor:

Copy code

class Person:
def __init__(self):
self.name = "John"
self.age = 30

person = Person()
print(person.name)
print(person.age)

Output:

Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 11-Jun-20 24.
John

30

In this example, the __init__ method is the default constructor for the Person
class. It is called automatically when the object is created, and it sets the default
values for the name and age attributes.

Parameterized Constructors
Parameterized constructors are useful when you want to create an object with
custom values for its attributes. They allow you to specify the values of the object’s
attributes when the object is created, rather than using default values.

Here is an example of a class with a parameterized constructor:

Copy code

class Person:
def __init__(self, name, age):
self.name = name
self.age = age

person = Person("Alice", 25)


print(person.name)
print(person.age)

Output:
Alice

25

In this example, the __init__ method is the parameterized constructor for the
Person class. It takes two arguments, name and age, and it sets the values of the

Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 11-Jun-20 24.
name and age attributes of the object to the values of these arguments.

Non-Parameterized Constructors
There is not necessarily a need for a non-parameterized constructor in Python. It is
up to the programmer to decide whether to include a non-parameterized
constructor in a class.

However, a non-parameterized constructor can be useful in the following cases:

When you want to initialize the def ault values f or the instance
variables of an object.
When you want to perf orm some operations when an object is
created, such as opening a f ile or establishing a connection to a
database.
When you want to create a “skeleton” object that can be used as a
template f or creating other objects.
For example, consider the following class:

Copy code

class MyClass:
def __init__(self):
self.arg1 = 10
self.arg2 = 20

In this case, the non-parameterized constructor is used to initialize the default


values for the instance variables arg1 and arg2. If you create an instance of the
MyClass class without passing any arguments, the default values will be used.

Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 11-Jun-20 24.
Copy code

class MyClass:
def __init__(self):
self.arg1 = 10
self.arg2 = 20

obj = MyClass()
print(obj.arg1)
print(obj.arg2)

Output:
10

20

Note: If you do not define a non-parameterized constructor in your class, Python


will automatically provide one for you. However, it is a good practice to define a
constructor for your class, even if it is a non-parameterized constructor so that you
have control over the initialization of your objects.

Rules of Python Constructor


Here are some rules for defining constructors in Python:

The constructor method must be named __init__. This is a special


name that is recognized by Python as the constructor method.
The f irst argument of the constructor method must be self . This is a
ref erence to the object itself , and it is used to access the object’s
attributes and methods.

Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 11-Jun-20 24.
The constructor method must be def ined inside the class def inition.
It cannot be def ined outside the class.
The constructor method is called automatically when an object is
created. You don’t need to call it explicitly.
You can def ine both def ault and parameterized constructors in a
class. If you def ine both, the parameterized constructor will be used
when you pass arguments to the object constructor, and the def ault
constructor will be used when you don’t pass any arguments.

Programming Online Courses Python Online Courses and


and Certif ication Certif ications
Data Science Online Courses Machine Learning Online Courses
and Certif ications and Certif ications

Multiple Constructors in Single Class


You can have more than one constructor in a single Python class. This is known as
“method overloading”. To do this, you will need to use the same method name (in
this case, the name of the method will be the same as the name of the class) but
define the method with different numbers or types of arguments.

Here is an example of a class with two constructors:

Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 11-Jun-20 24.
Copy code

class MyClass:
def __init__(self, arg1, arg2):
self.arg1 = arg1
self.arg2 = arg2

def __init__(self, arg1):


self.arg1 = arg1
self.arg2 = None

The first constructor takes two arguments and sets them as instance variables,
while the second constructor takes only one argument and sets it as an instance
variable. When you create an instance of the MyClass class, Python will use the
appropriate constructor based on the number of arguments that you pass.

For example:

Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 11-Jun-20 24.
Copy code

class MyClass:
def __init__(self, arg1, arg2):
self.arg1 = arg1
self.arg2 = arg2

def __init__(self, arg1):


self.arg1 = arg1
self.arg2 = None

obj1 = MyClass(10, 20)


obj2 = MyClass(30)

In the first case, the first constructor will be called and arg1 will be set to 10 and
arg2 will be set to 20. In the second case, the second constructor will be called and
arg1 will be set to 30 and arg2 will be set to None .
Note: Python does not have true method overloading like some other
programming languages. When you define multiple methods with the same name in
a single class, only the last one will be used. However, you can use default values for
arguments to achieve a similar effect.

Conclusion:
In this article, we have managed to cover the following concepts associated with
constructors in Python:

What are Constructors in Python?


Types of Constructors in Python

Rules of Python Constructor

Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 11-Jun-20 24.
Multiple Constructors in Single Class
Hope you will like the article.

FAQs

What is a constructor in Python?

What is the purpose of the self parameter in a constructor?

Can a class have multiple constructors in Python?

Can a constructor return a value in Python?

Is a constructor inherited by a subclass in Python?

Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 11-Jun-20 24.

You might also like