A constructor is a special method used to create and initialize an object of a class. On the other hand, a destructor is used to destroy the object.
After reading this article, you will learn:
- How to create a constructor to initialize an object in Python
- How
__new__()and__init__()work together - Different types of constructors
- Constructor overloading, alternative constructors, and constructor chaining
Table of contents
What is Constructor in Python?
In object-oriented programming, a constructor is a special method used to create and initialize an object of a class. This method is defined inside the class.
- The constructor is executed automatically when an object is created.
- The primary use of a constructor is to declare and initialize the data members (instance variables) of a class. The constructor contains a set of statements that run at the time of object creation to set the object’s attributes.
For example, when we execute obj = Sample(), Python knows that obj is an object of class Sample and calls the constructor of that class to create it.
Note: In Python, internally, the __new__() method creates the object, and the __del__() method is called to destroy the object when its reference count becomes zero.
In Python, building an object is divided into two steps: object creation and object initialization.
- Internally, the
__new__()method creates the object. - The
__init__()method then initializes it. This is the method we write to implement a constructor.
Syntax of a constructor
def __init__(self):
# body of the constructorCode language: Python (python)
Where,
def: The keyword used to define a function or method.__init__()method: A reserved method. Python calls it as soon as an object of the class is instantiated.self: The first argumentselfrefers to the current object. It binds the instance to the__init__()method. By convention, it’s namedself.
Note: The self parameter is required. Any parameters after self are up to you: a constructor can take zero, one, or many additional arguments.
Example: Create a Constructor in Python
In this example, we’ll create a class Student with an instance variable for the student’s name. We’ll see how to use a constructor to initialize the name at the time of object creation.
Output
Inside Constructor All variables initialized Hello, my name is Emma
- In the above example, the object
s1is created using the constructor. - While creating a
Studentobject,'Emma'is passed as an argument to the__init__()method to initialize the object. - Similarly, we can create more
Studentobjects by passing different names as arguments.

Note:
- For every object, the constructor is executed only once. For example, if we create four objects, the constructor is called four times.
- In Python, every class has a constructor, but you don’t have to define it explicitly. Defining a constructor in a class is optional.
- If you don’t define a constructor, Python uses a default one.
__new__() vs __init__()
When you write Student('Emma'), Python runs two methods in order:
__new__(cls)creates and returns a new, empty object. Its first parameter is the class itself.__init__(self)receives that new object asselfand sets its attributes.
You rarely need to override __new__(). It’s mainly used for special cases such as subclassing immutable types (like int or str) or implementing the Singleton pattern. The following example shows the order of the calls.
Output
1. __new__ called: creating the object 2. __init__ called: initializing the object Name: Emma
Types of Constructors
In Python, we have the following three types of constructors.
- Default constructor
- Non-parametrized constructor
- Parameterized constructor

Default Constructor
If you don’t define a constructor in your class, Python still lets you create objects of that class. In that case, it uses a default constructor: an empty constructor that takes no arguments (other than self) and doesn’t set any attributes.
Note:
- Python doesn’t add any code to your class. Every class inherits from Python’s built-in
objectclass, and the default constructor is simplyobject.__init__(), which is inherited automatically. - If you define your own
__init__(), it replaces the default one.
Example:
Output
Inside Display True
As you can see in the example, we don’t have a constructor, but we can still create an object of the class because Employee uses the default constructor inherited from object.
Non-Parametrized Constructor
A constructor without any arguments (other than self) is called a non-parameterized constructor. This type of constructor initializes each object with default values.
This constructor doesn’t accept any arguments during object creation. Instead, it initializes every object with the same set of values.
Output
Name: PYnative Address: ABC Street
As you can see in the example, we don’t pass any arguments to the constructor while creating the object.
Parameterized Constructor
A constructor with parameters is called a parameterized constructor. Using a parameterized constructor, we can pass different values to each object at the time of creation.
The first parameter of the constructor is self, which is a reference to the object being constructed. The rest of the arguments are provided by the programmer. A parameterized constructor can have any number of arguments.
For example, consider a company with thousands of employees. When creating each employee object, we need to pass a different name, age, and salary. In such cases, use a parameterized constructor.
Example:
Output
Emma 23 7500 Kelly 25 8500
In the above example, we define a parameterized constructor that takes three parameters: name, age, and salary.
Constructor With Default Values
Python allows us to define a constructor with default values. A default value is used when we don’t pass that argument to the constructor at the time of object creation.
The following example shows how to use default values with the constructor.
Example
Output
Emma 12 7 Kelly 13 7
As you can see, we didn’t pass the age and classroom values for Emma (or classroom for Kelly) at the time of object creation, so the default values are used.
Note: Don’t use a mutable object such as a list or dictionary as a default value (for example, def __init__(self, marks=[])). The same list would be shared by every object. Use None as the default and create the list inside the constructor instead.
Self Keyword in Python
A class contains instance variables and methods. Whenever we define an instance method in a class, we use self as the first parameter. Using self, we can access the instance variables and instance methods of the object.
The first argument self refers to the current object.
Whenever we call an instance method through an object, Python automatically passes the object reference as the first argument, which is commonly named self.
It is not mandatory to name the first parameter self. We can use any name we like, but it has to be the first parameter of an instance method. Using self is a strong convention, so stick to it.
Example
Output
Emma 12 Kelly 13
Constructor Overloading
Constructor overloading means having more than one constructor, each with a different parameter list, so that each constructor can perform a different task.
For example, in Java or C++ we can create three constructors that each accept a different set of parameters.
Python does not support constructor overloading. If we define multiple constructors, Python keeps only the last one, and it raises an error if the arguments don’t match that last constructor. The following example shows this.
Example
Output
TypeError: Student.__init__() missing 1 required positional argument: 'age'
- As you can see in the above example, we defined two constructors with different arguments.
- The second definition replaces the first, so the class has only the two-argument constructor.
- When we created an object with only one argument, we got a
TypeError.
Alternatives to Constructor Overloading
Python offers two simple ways to create objects in different ways:
- Default arguments: Make some parameters optional, so a single
__init__()handles different numbers of arguments. - Alternative constructors: Use a class method (
@classmethod) that builds the object from a different kind of input and then calls the regular constructor.
Output
Name: Emma | Age: None Name: Kelly | Age: 13 Name: Jessa | Age: 14
Constructor Chaining
Constructors are used to initialize an object. The job of the constructor is to assign values to the data members when an object of the class is created.
Constructor chaining is the process of calling one constructor from another constructor. It’s useful when you want several constructors to run, one after another, while creating a single object.
In Python, constructor chaining is most useful with inheritance. Each child class constructor calls its parent class constructor first, so the parent constructors run before the child’s own initialization code.
Using the super() function, we can invoke the parent class constructor from a child class.
Note: Python doesn’t call the parent constructor automatically. If a child class defines __init__() and doesn’t call super().__init__(), the parent’s attributes won’t be set.
Example
Output
Inside Vehicle Constructor Inside Car Constructor Inside Electric Car Constructor Engine=1500cc, Max Speed=240, Km range=750
Counting the Number of objects of a Class
The constructor runs every time we create an object of the class, and only once per object. So to count the number of objects of a class, we can keep a counter in a class variable and increment it by one in the constructor.
Example
Output
The number of Employee: 3
Constructor Return Value
In Python, the constructor doesn’t return any value, so we don’t declare a return type for it. The constructor is called implicitly at the time of object instantiation, and its only purpose is to initialize the instance variables.
The __init__() method must return None. If we try to return any other value from __init__(), Python raises a TypeError.
Example
Output
TypeError: __init__() should return None, not 'bool'
Conclusion and Quick recap
In this lesson, we learned what constructors are and how to use them in object-oriented programming to design classes and create objects.
The list below summarizes the concepts covered in this tutorial.
- A constructor is a special method used to initialize an object of a class. In Python, it is the
__init__()method. - Object creation happens in two steps:
__new__()creates the object, and__init__()initializes it. - If no constructor is defined, Python uses the default constructor inherited from the
objectclass. - The constructor must return
None. Returning any other value raises aTypeError. - Python has three types of constructors: default, non-parameterized, and parameterized.
- Using
self, we can access the instance variables and instance methods of the object. The first argumentselfrefers to the current object. - Constructor overloading isn’t supported in Python. Use default arguments or
@classmethodalternative constructors instead. - A child class constructor can invoke the parent class constructor using
super().__init__().
