01 Jun Python Constructors: All You Need to Know
Photo by Clément Hélardot on Unsplash
Constructors in Python are methods used for initializing an object. We can say they are methods called whenever an object is created. A constructor also has one simple task, which is to assign values to data members of a class whenever an object has been created.
In explaining constructor better, a constructor is called by the __init__()
method, and it is invoked whenever an object is created. For instance, if we create three objects, our class constructor will be called by that same three times.
Now, let’s discuss the different types of constructors we have in Python;
Types Of Constructors
We have two types of Constructors and they are:
- Default constructor
- Parameterized constructor
Default Constructor
The default constructor is the type of constructor that takes no arguments. This constructor holds only one argument “self”, which is a reference to the instance that is being established.
Example 1
In the example below, we created a class named Fruits and defined a constructor that takes no arguments. Inside the constructor, we initialized two variables known as “first_fruit” and “second_fruit”. Then we created a method for printing our data members and also created an objects of the class named “our_object”. Using these objects, we were able to call our instance method.
1 class Fruits: 2 3 # our default constructor 4 def __init__(self): 5 self.first_fruit = "Pineapple" 6 self.second_fruit ="Apple" 7 8 # we define a method for printing our data members 9 def print_fruit(self): 10 print(self.first_fruit) 11 print(self.second_fruit) 12 13 14 # created object of the class here 15 our_object = Fruits() 16 17 # let's call the instance method using our object of the class 18 our_object.print_fruit()
Output
We generated the output below;
1 Pineapple
2 Apple
Let’s look at this other example below on Default Constructor. In our example below, we can observe that it has no argument as well.
Example 2
1 class Customer: 2 3 #our default constructor 4 def __init__(self): 5 self.name = "Scott" 6 self.age = 40 7 self.address = "Elizabeth strt" 8 9 # we define a method for printing our data members 10 def details(self): 11 print('Name:', self.name, 'Age:', self.age,'Address:', self.address) 12 13 # created object of the class here 14 the_cust= Customer() 15 16 # let's call the instance method using our object of the class 17the_cust.details()
Output
We generated this output.
1 Name: Scott Age: 40 Address: Elizabeth strt
2
Parameterized constructor
This constructor contains parameters just as the name depicted. It has a reference to the instance being created called self
just like that of the default constructor but here, it can allow more arguments and the self serves as the first argument.
Let’s dive in different examples on Parameterized constructors to understand it better.
Example 1
In this example, we created a Customer class with constructor that has one more argument apart from the self, which acts as the first argument. Here, we can see how we used the “name” arguments after we created the object of our class. We can observe that the name (Mr Scott) was passed in as an argument when we created the object.
1 class Customer: 2 # Our parameterized Constructor with more argument 3 def __init__(self, name): 4 self.name = name 5 self.age = 40 6 7 # we define a method for printing our data members 8 def details(self): 9 print(“Hi “,self.name) 10 11 # created object of the class here 12 the_cust = Customer(“Mr Scott”) 13 14 # let’s call the instance method using our object of the class 15the_cust.details()
Output
We should have this output after running our code
1 Hi Mr Scott
Let’s check one more example on the Parameterized Constructor
Example2
Here is also another way of defining more than one arguments in a constructor just as seen below
1 class Customer: 2 # parameterized constructor 3 def __init__(self, name, age, address): 4 self.name = name 5 self.age = age 6 self.address = address 7 8 # display object 9 def details(self): 10 print(self.name, self.age, self.address) 11 12 # creating object of the Employee class 13 Mike = Customer('Mike Howard', 40, '520 N MADISON LOS ANGELES') 14 Rose = Customer('Rose Cooper', 32, '521 N MADISON LOS ANGELES') 15 Benjamin = Customer('Benjamin Smith',35,'522 N MADISON LOS ANGELES') 16 Mike.details() 17 Rose.details() 18 Benjamin.details()
Output
1 Mike Howard 40 520 N MADISON LOS ANGELES 2 Rose Cooper 32 521 N MADISON LOS ANGELES 3 Benjamin Smith 35 522 N MADISON LOS ANGELES
Hope you enjoyed this post. If you do, Comment, share, and follow for more informative posts. Thanks for reading.
No Comments