| Python Constructor
PYTHON CONSTRUCTOR
CONSTRUCTOR
• A special method in python, which is used to initialize the data
members of a class.
• It will be called automatically, when an object of a class is created.
• Unlike java/c#, the constructors need not have the same name as
the class name itself.
• In python, the constructor is defined using the special method init()
which is prefixed and suffixed with double underscore operators
__init__()
• Generally, it is used for creating an object
• It is an optional. (Alternative to methods)
SYNTAX
Non-Parameterized Constructor (Default Constructor)
def __init__(self):
# code
Where,
• First argument is the current object. It is usually named as self to
follow the naming convention.
• So, you can give any user defined name for first parameter.
Parameterized Constructor
def __init__(self, a1, a2, …an):
# code
Where,
• First argument is the current object which is self
• Remaining arguments are user defined
1
| Python Constructor
Example
class Test:
def __init__(self): # non-parameterized constructor
# user code
# object creation and calling constructor
Obj=Test()
Characteristics
• It has no dot operator and no return type
• It will be called automatically, when the object is created
• It is an important to note that, it does not return anything except
None.
• Python does not support multiple constructors in the same class.
If we can define multiple constructors, but the last one will override the
earlier constructor definitions.
Usage
• It is used to initialize the fields of a class
• It can be used to allocate the memory to the fields during object
creation.
DIFFERENCE BETWEEN CONSTRUCTOR AND METHOD
S.N Constructor Method
1. Special method Normal method
2. Implicit Calling Explicit Calling
• It will be called automatically, • Method must be called by
when the object of a class is using object / class.
created.
3. It returns only None type It returns any type
2
| Python Constructor
DIFFERENCE BETWEEN PYTHON CONSTRUCTOR AND C++/JAVA/C#
CONSTRUCTORS
S.N Python Constructor C++/C#/Java Constructors
1. Here class name and constructor Here class name and
name should not be same. It is constructor name should be
defined using the special method same
like __init()__
2. Here constructor should take one This is not applicable in
argument which is usually self in c++/c#/java.
both parameterized and non-
parameterized constructor
3. It is an optional and return only Here it does not return
None type anything
TYPES
• Like c++/java, python provides two types of constructors. They are:
1. Parameter less Constructor (Default Constructor)
2. Parametrized Constructor
Constructors Types
Parameter less Parameterized
Constructor Constructor
Figure: Types of Constructors
3
| Python Constructor
1. PARAMETER LESS CONSTRUCTOR (DEFAULT CONSTRUCTOR)
• If a constructor doesn’t take any arguments, then it is called as default
constructor / parameter less constructor.
• Unlike c++/java/c#, python should include self keyword as a first
argument which is mandatory. Self can any user defined name.
Use
• It is used to initialize the data members (variables) of a
class.
Calling
• It will be called automatically, when an object of a class
is created. (Implicit Calling)
• Implicit calling (No need to use an object to call constructor)
4
| Python Constructor
I. EXAMPLE OF DEFAULT CONSTRUCTOR
(emp.py)
1. SOURCE CODE Default Constructor
class Person:
def __init__(self):
self.name="Sachin" Public Instance Variables
self.id=15
def disp(self):
Instance Method
print("Name\t: ",self.name)
print("Id\t\t: ",self.id)
# object creation
Object Creation & Calling
print("-------------------------------------") Default Constructor
print("\t Python Default Constructor")
print("-------------------------------------")
obj=Person()
obj.disp()
2. OUTPUT
5
| Python Constructor
2. PARAMETERIZED CONSTRUCTOR
• If constructor accepts or takes arguments, then it is called as
parameterized constructor / argument constructor.
• The parameterized constructors are called by using its signatures.
• Signature can be number of arguments / type of arguments
• The constructor supports only number-based arguments (not types)
II. EXAMPLE OF PARAMETERIZED CONSTRUCTOR
(Stud.py)
1. SOURCE CODE Parameterized Constructor
class Student:
def __init__(self,pname, pid):
self.name=pname Public Instance Variables
self.id=pid
def disp(self):
Instance Method
print("Name\t: ",self.name)
print("Id\t\t: ",self.id)
# object creation
print("-------------------------------------------")
print("\t Python Parameterized Constructor")
print("-------------------------------------------")
obj=Student("Krishna",5)
obj.disp()
Object Creation & Calling
Parameterized Constructor
6
| Python Constructor
2. OUTPUT