Nearby lessons
100 of 159Python - Constructor
- Understand what a constructor is and its purpose
- Use the __init__() method to initialize instance variables
- Know that a constructor executes only once per object
- Explain why Python does not support traditional constructor overloading
- Implement constructor overloading with default and variable length arguments
What Is a Constructor?
A constructor is a special method used to initialize an object.
In Python, the constructor method is named __init__.
Why Is It Used?
Constructors are mainly used to declare and initialize instance variables when an object is created.
For every object, the constructor is executed automatically and only once.
Constructor Example
Explanation
The constructor __init__(self, name, rollno, marks) accepts three values.
When each object is created, the constructor executes automatically and stores the values inside that object using self.
Each call to Student(...) runs the constructor once, so the constructor executes twice for the two objects created above.
Constructor Executes Only Once Per Object
The following program shows that the constructor executes once for every object created, while a method executes only when it is explicitly called.
Differences Between Methods and Constructors
| Method | Constructor |
|---|---|
| The name of a method can be any name. | The constructor name should always be __init__. |
| A method executes only if we call it. | A constructor executes automatically when an object is created. |
| A method can be called any number of times for the same object. | A constructor is executed only once for each object. |
| A method is used to perform business logic or operations. | A constructor is mainly used to declare and initialize instance variables. |
Constructor Overloading
Constructor overloading means defining multiple constructors with different numbers of arguments.
However, Python does not support constructor overloading. If multiple constructors are declared, only the last constructor is available.
Explanation
Only the Two-Arg Constructor remains available because the latest __init__() definition replaces all previous definitions.
Calling Test() or Test(10) would raise a TypeError because the available constructor requires two arguments.
Constructor with Default Arguments
Based on our requirement, we can declare a constructor with default arguments.
By giving default values to the parameters, the same constructor can be used with zero, one, two, or three arguments.
Constructor with Variable Number of Arguments
We can also declare a constructor with a variable number of arguments using the *a parameter.
When *a is used, all the values passed while creating the object are collected into a tuple, so the same constructor can accept any number of arguments.
Comparison of Constructor Types
| Constructor Type | Number of Arguments |
|---|---|
| Normal Constructor | Fixed number of arguments |
| Constructor with Default Arguments | 0, 1, 2, or 3 arguments (as defined) |
| Constructor with Variable Arguments | Any number of arguments |
- A constructor is a special method named __init__()
- It runs automatically when an object is created and executes only once per object
- Its main purpose is to declare and initialize instance variables
- The constructor is optional; Python provides a default constructor if none is defined
- Python keeps only the last constructor, so overloading is achieved with default or *args arguments