- Learning Objectives
- Prerequisites
- Introduction
- What is a Constructor?
- Types of Constructors
- What is Constructor Overloading?
- Does Python Support Constructor Overloading?
- Why Python Does Not Support Constructor Overloading
- How Python Achieves Constructor Overloading
- Default Arguments
- Variable-Length Arguments (*args)
- Keyword Arguments (**kwargs)
- Constructor Overloading vs Method Overloading
- Internal Working
- Summary
After completing this chapter, you will be able to:
- Understand Constructors.
- Understand Constructor Overloading.
- Explain why Python does not support true Constructor Overloading.
- Implement Constructor Overloading using Python techniques.
- Differentiate Constructor Overloading and Method Overloading.
- Write efficient constructors using default arguments, *args and **kwargs.
Before learning this chapter, you should know:
- Classes
- Objects
- Constructors
- Methods
- Static Variables
Whenever an object is created, Python automatically initializes it using a special method called the Constructor.
Sometimes, we want to initialize objects in different ways.
For example,
A Student object may be created using
- Name
- Name and Age
- Name, Age and Course
- Name, Age, Course and Marks
In Java, this can be achieved using Constructor Overloading.
Python follows a different approach.
Instead of supporting multiple constructors,
Python provides flexible mechanisms such as
- Default Arguments
- Variable-Length Arguments (*args)
- Keyword Arguments (**kwargs)
These features eliminate the need for true Constructor Overloading.
A Constructor is a special method that is automatically executed when an object is created.
In Python,
the constructor is
__init__()Its primary purpose is to initialize the object's data.
Example
class Student:
def __init__(self):
print("Constructor Executed")Whenever an object is created,
the constructor executes automatically.
Python mainly provides two types of constructors.
A constructor that does not require additional arguments.
Example
class Student:
def __init__(self):
print("Default Constructor")A constructor that accepts arguments during object creation.
Example
class Student:
def __init__(self, name):
self.name = nameConstructor Overloading is the process of creating multiple constructors with the same name but different parameter lists.
Example (Java)
Student()
Student(String name)
Student(String name, int age)
Student(String name, int age, String course)The compiler selects the appropriate constructor depending on the arguments passed.
No.
Python does not support true Constructor Overloading.
Only one constructor named
__init__()can exist inside a class.
If multiple constructors are defined,
only the last one remains.
class Student:
def __init__(self):
print("First Constructor")
def __init__(self, name):
print("Second Constructor")Only the second constructor exists.
The first constructor is overwritten.
Python is a dynamically typed language.
Unlike Java,
Python does not select methods or constructors based on parameter signatures.
Instead,
Python encourages flexible programming using
- Default Arguments
- *args
- **kwargs
This makes multiple constructors unnecessary.
Although Python does not support true Constructor Overloading,
similar behavior can be achieved using:
- Default Arguments
- Variable-Length Arguments (
*args) - Keyword Arguments (
**kwargs)
Default arguments allow parameters to become optional.
Example
class Student:
def __init__(self, name, age=18):
self.name = name
self.age = ageObjects
Student("Rahul")
Student("Priya", 20)Both statements are valid.
*args accepts any number of positional arguments.
Example
class Student:
def __init__(self, *args):
print(args)Possible object creation
Student()
Student("Rahul")
Student("Rahul", 21)
Student("Rahul", 21, 95)**kwargs accepts any number of keyword arguments.
Example
class Student:
def __init__(self, **kwargs):
print(kwargs)Example
Student(name="Rahul")
Student(name="Rahul", age=21)
Student(name="Rahul", age=21, course="Python")| Constructor Overloading | Method Overloading |
|---|---|
| Uses Constructors | Uses Methods |
| Object Initialization | General Operations |
| Not directly supported in Python | Not directly supported in Python |
| Simulated using Default Arguments, *args and **kwargs | Simulated using Default Arguments, *args and **kwargs |
Object Created
↓
Python Searches
↓
__init__()
↓
Constructor Executes
↓
Object Initialized
If multiple constructors exist,
Python simply keeps the last constructor definition.
- Flexible object creation.
- Less code duplication.
- Easier maintenance.
- Supports optional parameters.
- Cleaner class design.
Constructor initialization is used in:
- Student Management Systems
- Banking Applications
- Employee Management Systems
- Inventory Systems
- E-Commerce Applications
- Hospital Management Systems
In this part, we learned:
- Constructors
- Types of Constructors
- Constructor Overloading
- Why Python does not support Constructor Overloading
- Default Arguments
- *args
- **kwargs
- Constructor Overloading vs Method Overloading
- Internal Working
Write a Python program to demonstrate that Python does not support true Constructor Overloading.
class Student:
def __init__(self):
print("Default Constructor")
def __init__(self, name):
print("Parameterized Constructor")
print("Name :", name)
student = Student("Rahul")Parameterized Constructor
Name : Rahul
Although two constructors are defined,
Python keeps only the last __init__() method.
The first constructor is overwritten.
Write a Python program to simulate Constructor Overloading using default arguments.
class Student:
def __init__(self, name, age=18):
self.name = name
self.age = age
print("Name :", self.name)
print("Age :", self.age)
student1 = Student("Rahul")
student2 = Student("Priya", 20)Name : Rahul
Age : 18
Name : Priya
Age : 20
If the age is not provided,
Python automatically uses the default value.
This simulates Constructor Overloading.
Write a Python program using *args.
class Student:
def __init__(self, *args):
print("Arguments :", args)
Student()
Student("Rahul")
Student("Rahul", 21)
Student("Rahul", 21, "Python")Arguments : ()
Arguments : ('Rahul',)
Arguments : ('Rahul', 21)
Arguments : ('Rahul', 21, 'Python')
*args accepts any number of positional arguments.
It stores all arguments inside a tuple.
class Student:
def __init__(self, **kwargs):
print(kwargs)
Student(name="Rahul")
Student(name="Rahul", age=21)
Student(name="Rahul", age=21, course="Python"){'name': 'Rahul'}
{'name': 'Rahul', 'age': 21}
{'name': 'Rahul', 'age': 21, 'course': 'Python'}
**kwargs accepts any number of keyword arguments.
It stores them inside a dictionary.
class Student:
def __init__(self, name="Unknown", *marks):
print("Name :", name)
print("Marks :", marks)
Student()
Student("Rahul")
Student("Priya", 90, 85, 95)Name : Unknown
Marks : ()
Name : Rahul
Marks : ()
Name : Priya
Marks : (90, 85, 95)
The first parameter uses a default value.
Additional values are stored inside marks.
class Employee:
def __init__(self, department="General", **details):
print("Department :", department)
print("Details :", details)
Employee()
Employee("HR", name="Rahul", salary=50000)Department : General
Details : {}
Department : HR
Details : {'name': 'Rahul', 'salary': 50000}
Default arguments and **kwargs can work together to initialize objects flexibly.
class Student:
def __init__(self, name, age=18, course="Python"):
self.name = name
self.age = age
self.course = course
def display(self):
print(self.name)
print(self.age)
print(self.course)
student = Student("Rahul")
student.display()Rahul
18
Python
Default values allow objects to be created with fewer arguments.
class Student:
def __init__(self, name, *subjects):
self.name = name
self.subjects = subjects
print("Student :", self.name)
print("Subjects :", self.subjects)
Student("Rahul", "Python", "SQL", "Java")Student : Rahul
Subjects : ('Python', 'SQL', 'Java')
class Employee:
def __init__(self, **details):
for key, value in details.items():
print(key, ":", value)
Employee(name="Rahul", age=25, department="HR")name : Rahul
age : 25
department : HR
**kwargs is useful when the number of keyword arguments is unknown.
class Product:
def __init__(self, *args, **kwargs):
print("Positional :", args)
print("Keyword :", kwargs)
Product("Laptop", 50000, brand="Dell", ram="16GB")Positional : ('Laptop', 50000)
Keyword : {'brand': 'Dell', 'ram': '16GB'}
A constructor can accept both positional and keyword arguments, making it highly flexible.
Object Created
↓
Arguments Passed
↓
__init__() Called
↓
Arguments Matched
↓
Object Initialized
↓
Constructor Ends
Student Object
│
▼
+---------------------------+
name
↓
Rahul
age
↓
21
course
↓
Python
+---------------------------+
- Flexible object creation.
- Reduces duplicate constructors.
- Supports optional parameters.
- Cleaner class design.
- Easier maintenance.
- Use default arguments when only a few parameters are optional.
- Use
*argswhen the number of positional arguments is unknown. - Use
**kwargsfor optional keyword arguments. - Keep constructors simple.
- Validate constructor arguments whenever necessary.
Writing multiple constructors.
❌ Incorrect
class Student:
def __init__(self):
pass
def __init__(self, name):
passOnly the last constructor exists.
Using *args unnecessarily.
If the number of parameters is known,
use normal parameters.
Using **kwargs without validation.
Always verify required keys before using them.
Making constructors overly complex.
Constructors should initialize objects,
not contain business logic.
A Constructor is a special method (__init__()) that automatically executes when an object is created.
Constructor Overloading means creating multiple constructors with different parameter lists.
No.
Python allows only one __init__() method per class.
Python is dynamically typed and instead provides flexible features such as default arguments, *args, and **kwargs.
Using:
- Default Arguments
*args**kwargs
A tuple containing positional arguments.
A dictionary containing keyword arguments.
Only the last definition remains.
In Python, default arguments are preferred because they provide flexible object initialization without multiple constructors.
Student Management Systems, Banking Applications, Employee Management Systems, Inventory Systems, and E-Commerce Applications.
- Create a Student class using default arguments.
- Create an Employee class using
*args. - Create a Product class using
**kwargs. - Create a constructor accepting both
*argsand**kwargs. - Demonstrate why multiple constructors do not work in Python.
- Constructor →
__init__() - Python supports only one constructor.
- Use default arguments for optional parameters.
*argsstores positional arguments in a tuple.**kwargsstores keyword arguments in a dictionary.- Python simulates Constructor Overloading instead of supporting it directly.
In this chapter, you learned:
- Constructors
- Constructor Overloading
- Why Python does not support true Constructor Overloading
- Default Arguments
*args**kwargs- Practical Programs
- Best Practices
- Common Mistakes
- Interview Questions
- Practice Programs
- Quick Revision