Constructor Type in Python
Constructor in Python
Python Constructor in object- acquainted programming with Python is a one kind of approach/ function we utilize to initialize case components of that class.
Rules of Python Constructor
It starts with the def keyword like all distinguishable functions in Python.
It’s supervened by the expression init which is prefixed and suffixed with binary underscores with a couple of brackets i.e.,, init, ().
It takes an explanation called as self with which it assigns valuations to the variables.
Self is a source to the customary case of the class. It’s created and reached automatically/ implicitly to the, init, () when the constructor is called.
Types of constructors
Default constructor
The default constructor is a plain constructor which does n’t undertake any arguments. Its portrait has only one argument which is a reference to the case being contrived.
Parameterized constructor
constructor with parameters is known as parameterized constructor. The parameterized constructor takes its premier argument as a reference to the sample being constructed known as self and the rest of the arguments are delivered by the programmer.
Example
Get tech skill’s stories in your inbox
Join Medium for free to get updates from this writer.
class teacher:
# Constructor — parameterized
def __init__(self, name):
print(“This is parametrized constructor”)
self.name = name
def show(self):
print(“Hii”,self.name)
teacher = teacher(“Leena”)
teacher.show()
Output
This is parameterized constructor
Hii Leena
Non-parameterized constructor
When we need a constructor to serve entity but none of that’s to exploit values, we can employ anon-parameterized constructor.
Lets see the example :
class teacher:
# Constructor — non parameterized
def __init__(self):
print(“This is non parametrized constructor”)
def show(self,name):
print(“Hii”,name)
teacher = teacher()
teacher.show(“Leena”)
Summary
So here, we learned about the constructors rules , types of constructor default , parameterized constructor and non-parameterized constructor .
Learn more about constructor overloading in python in our blog here .








