In this tutorial, we’ll learn the concepts of constructors in Python. A constructor is a special method of a class in the Object-Oriented Programming which gets invoked automatically whenever an object of that class is created.
Ways to use constructor?
Constructors are primarily used to initialise the class data members to each object at the time of object instantiation. In Python, Constructors are declared using __init__()
method which takes self as the first argument.
Types of constructors in Python
Default Constructor – This kind of constructor does not contain any parameter except self. Even if we do not declare any constructor in our class, a default constructor is automatically invoked but it is a good practice to involve a default constructor even if we do not require it.
Parametrised Constructor – This takes one or more arguments along with self to initialise a value to the class members to the objects.
Imagine we are a car collector and wants to keep a track of the cars information that we add in our collection and the count of the cars. It is not efficient to do all this manually. Let us implement this using class & constructor so that we can add the cars in our collection efficiently.
Default Constructor:
class Garage:
make = None
model = None
color = None
def __init__(self):
pass
car1 = Garage()
car2= Garage()
car2.model = "x1"
print("Car 1 make: ",car1.make)
print("Car 1 model: ",car1.model)
print("Car 2 model: ",car2.model)
Output:
Car 1 make: None
Car 1 model: None
Car 2 model: x1
We see that a default constructor will use default data member values for each object & we have to manually initialize the different values for class data members as we did for car2.model
. Let’s use the parametrised constructor to avoid this.
Parametrised Constructor:
class Garage:
make = None
model = None
color = None
def __init__(self,make,model,color):
self.make=make
self.model=model
self.color=color
car1 = Garage("BMW","X1","Black")
car2= Garage("Honda","City","Silver")
print("Details of Car 1: \n",car1.__dict__)
print("Details of Car 2: \n",car2.__dict__)
Output:
Details of Car 1:
{'make': 'BMW', 'model': 'X1', 'color': 'Black'}
Details of Car 2:
{'make': 'Honda', 'model': 'City', 'color': 'Silver'}
Here, we have passed the class data members to the constructor so that whenever an object is created for this class with the appropriate number of arguments, then they will be automatically initialized. We have used __dict__
class attribute to output the information of the objects.
Tracking number of objects created:
We can keep a track of the number of objects created by increment a data member inside the constructor. Note that the number of cars in a garage is a property of garage and does not depend upon each car, we will make a change in class member by using the name of the class rather than self
keyword.
class Garage:
make = None
model = None
color = None
numCars = 0
def __init__(self,make,model,color):
self.make=make
self.model=model
self.color=color
Garage.numCars = Garage.numCars+1
car1 = Garage("BMW","X1","Black")
car2= Garage("Honda","City","Silver")
car3 = Garage("Toyota","Fortuner","White")
print("Details of Car 1: \n",car1.__dict__)
print("Details of Car 2: \n",car2.__dict__)
print("Details of Car 3: \n",car3.__dict__)
print("Number of Cars in Garage: ",Garage.numCars)
Output:
Details of Car 1:
{'make': 'BMW', 'model': 'X1', 'color': 'Black'}
Details of Car 2:
{'make': 'Honda', 'model': 'City', 'color': 'Silver'}
Details of Car 3:
{'make': 'Toyota', 'model': 'Fortuner', 'color': 'White'}
Number of Cars in Garage: 3
So we learn about what are constructors, types of constructors, and how to use them with example.
Reference:
Happy Learning 🙂