Constructors in Python: Definition, Types, and Rules: Vikram Singh
Constructors in Python: Definition, Types, and Rules: Vikram Singh
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.
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
Copy code
class Person:
def __init__(self, name, age):
self.name = name
self.age = 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.
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
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.
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.
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.
Copy code
class Person:
def __init__(self, name, age):
self.name = name
self.age = 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.
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
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
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.
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
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
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:
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
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.