Constructor in Object Oriented Programming (OOPs) in Python | Python Programming
Constructor: Constructor are just a special type of method. It is called automatically during object creation of a class.Constructors are used generally to initializing the value. The constructor is called automatically, when an object of class is created. Constructor does the work to initialize or assign the values to the members (class variables and class methods) of the class. The method named __init__() is the constructor in python. It is called always when an object is created.
Syntax of creating constructor:
#Syntax of defining constructor in python def __init__(self): # body of the constructor
Types of Constructors: There are two types of constructors.
1). Default Constructor
2). Parameterized Constructor
(i). Default Constructor: Default constructor is the simple and normal constructor. It does not hold any external parameter. Or we can say that it does not accept any arguments as a parameter. It only holds or accept self argument which is use for passing the object reference itself. Instance variables of a class are initialize in the constructor. Now before we didn’t talked about instance variable. So let’s make it clear first. Mainly there are two types of variable in a class ie.
There are two types of variable in a class
1). Class variable
2). Instance variable
(i). Class variable: Class variable are the normal variables as we used in procedural programming. It is always created inside the class and outside the class constructor.
(ii). Instance variable: Instance variable are the variables which are defined inside the methods and constructor. These methods and constructors are defined inside the class, so its also right if we say that instance variables are initialized inside the class but not outside of methods or constructors. These variables are created by variables names followed by “self” keyword.
Below given example make you clear to differentiate between “Class variable” and “Instance variable”.
So this is the difference between class variable and instance variable. Let’s not distract to our main topic ie. “Constructor in Python”.
I’m going to write same definition of constructor just to make the flow of our learning.
(i). Default Constructor: Default constructor is the simple and normal constructor. It does not hold any external parameter. Or we can say that it does not accept any arguments as a parameter. It only holds or accept self argument which is use for passing the object reference itself. Instance variables of a class are initialize in the constructor.
Note: If you want to understand the concept of constructor deeply then please follow the both example with your 100% of focus.
Example 1: Simple example on default constructor. In this example, we are creating the constructor
#Default Constructor example class pythonlobby: #empty default constructor #def __init__(self): # pass #default constructor def __init__(self): self.lobby = "Pythonlobby" #method for printing data member def prints(self): print(self.lobby) #creating object obj = pythonlobby() #calling the instance method using object obj obj.prints()
Example 2: Same example like the above, but in this example we are not going to create any constructor. Why?
#Constructor example class pythonlobby: lobby = "PythonLobby" #class variable #method for printing data member def prints(self): print(self.lobby) #creating object obj = pythonlobby() #calling the instance method using object obj obj.prints()
As per the definition and explanation, instance variable are initialized only by constructor. But in above example we do not created any constructor. Then how instance variable is initialized inside our method. This is because:-
Note: No matter we define constructor or not, When we create the object of a class a default constructor is created automatically by the compiler implicitly.
Explanation: In above example we created the object, at the time of object creation, a default constructor is created automatically and that default constructor initialize the instance variable implicitly.
Actually the constructor is always there in our class, but it was hidden. We just override the constructor according to our program need. Now next is parameterized constructor.