PYnative

Practice Coding

  • Python â–¼
    • Python Tutorials
    • Python Basics
    • Python Interview Q&As
  • Coding Exercises â–¼
    • Python Exercises
    • C++ Exercises
    • C Programming Exercises
    • Java Exercises
    • C# Exercises
  • MCQsâ–¼
    • Python MCQ Quizzes
    • Java MCQ Quizzes
    • C++ MCQ Quizzes
  • Compilersâ–¼
    • Online Python Compiler
    • Online C Compiler
    • Online C++ Compiler
    • Online Java Compiler
    • Online C# Compiler
Home » Python » Python Object-Oriented Programming (OOP) » Constructors in Python

Constructors in Python

Updated on: December 4, 2026 | 10 Comments

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?
    • Example: Create a Constructor in Python
    • __new__() vs __init__()
  • Types of Constructors
    • Default Constructor
    • Non-Parametrized Constructor
    • Parameterized Constructor
  • Constructor With Default Values
  • Self Keyword in Python
  • Constructor Overloading
    • Alternatives to Constructor Overloading
  • Constructor Chaining
  • Counting the Number of objects of a Class
  • Constructor Return Value
  • Conclusion and Quick recap

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 argument self refers to the current object. It binds the instance to the __init__() method. By convention, it’s named self.

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.

class Student:

    # constructor
    # initialize instance variable
    def __init__(self, name):
        print('Inside Constructor')
        self.name = name
        print('All variables initialized')

    # instance Method
    def show(self):
        print('Hello, my name is', self.name)


# create object using constructor
s1 = Student('Emma')
s1.show()Code language: Python (python)

Output

Inside Constructor
All variables initialized
Hello, my name is Emma
  • In the above example, the object s1 is created using the constructor.
  • While creating a Student object, 'Emma' is passed as an argument to the __init__() method to initialize the object.
  • Similarly, we can create more Student objects by passing different names as arguments.
Create object in Python using a constructor
Create an object in Python using a constructor

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:

  1. __new__(cls) creates and returns a new, empty object. Its first parameter is the class itself.
  2. __init__(self) receives that new object as self and 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.

class Student:
    def __new__(cls, *args, **kwargs):
        print('1. __new__ called: creating the object')
        instance = super().__new__(cls)
        return instance

    def __init__(self, name):
        print('2. __init__ called: initializing the object')
        self.name = name

s1 = Student('Emma')
print('Name:', s1.name)Code language: Python (python)

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
Types of constructor
Types of 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 object class, and the default constructor is simply object.__init__(), which is inherited automatically.
  • If you define your own __init__(), it replaces the default one.

Example:

class Employee:

    def display(self):
        print('Inside Display')

emp = Employee()
emp.display()

# the class uses the constructor inherited from object
print(Employee.__init__ is object.__init__)
Code language: Python (python)

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.

class Company:

    # no-argument constructor
    def __init__(self):
        self.name = "PYnative"
        self.address = "ABC Street"

    # a method for printing data members
    def show(self):
        print('Name:', self.name, 'Address:', self.address)

# creating object of the class
cmp = Company()

# calling the instance method using the object
cmp.show()Code language: Python (python)

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:

class Employee:
    # parameterized constructor
    def __init__(self, name, age, salary):
        self.name = name
        self.age = age
        self.salary = salary

    # display object
    def show(self):
        print(self.name, self.age, self.salary)

# creating object of the Employee class
emma = Employee('Emma', 23, 7500)
emma.show()

kelly = Employee('Kelly', 25, 8500)
kelly.show()
Code language: Python (python)

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

class Student:
    # constructor with default values age and classroom
    def __init__(self, name, age=12, classroom=7):
        self.name = name
        self.age = age
        self.classroom = classroom

    # display Student
    def show(self):
        print(self.name, self.age, self.classroom)

# creating object of the Student class
emma = Student('Emma')
emma.show()

kelly = Student('Kelly', 13)
kelly.show()
Code language: Python (python)

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

class Student:
    # constructor
    def __init__(self, name, age):
        self.name = name
        self.age = age

    # self points to the current object
    def show(self):
        # access instance variable using self
        print(self.name, self.age)

# creating first object
emma = Student('Emma', 12)
emma.show()

# creating Second object
kelly = Student('Kelly', 13)
kelly.show()
Code language: Python (python)

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

class Student:
    # one argument constructor
    def __init__(self, name):
        print("One argument constructor")
        self.name = name

    # two argument constructor
    def __init__(self, name, age):
        print("Two arguments constructor")
        self.name = name
        self.age = age

# creating first object
emma = Student('Emma')

# creating Second object
kelly = Student('Kelly', 13)
Code language: Python (python)

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.
class Student:
    def __init__(self, name, age=None):
        self.name = name
        self.age = age

    # alternative constructor
    @classmethod
    def from_string(cls, data):
        name, age = data.split('-')
        return cls(name, int(age))

    def show(self):
        print('Name:', self.name, '| Age:', self.age)

emma = Student('Emma')
kelly = Student('Kelly', 13)
jessa = Student.from_string('Jessa-14')

emma.show()
kelly.show()
jessa.show()Code language: Python (python)

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

class Vehicle:
    # Constructor of Vehicle
    def __init__(self, engine):
        print('Inside Vehicle Constructor')
        self.engine = engine

class Car(Vehicle):
    # Constructor of Car
    def __init__(self, engine, max_speed):
        super().__init__(engine)
        print('Inside Car Constructor')
        self.max_speed = max_speed

class ElectricCar(Car):
    # Constructor of Electric Car
    def __init__(self, engine, max_speed, km_range):
        super().__init__(engine, max_speed)
        print('Inside Electric Car Constructor')
        self.km_range = km_range

# Object of electric car
ev = ElectricCar('1500cc', 240, 750)
print(f'Engine={ev.engine}, Max Speed={ev.max_speed}, Km range={ev.km_range}')
Code language: Python (python)

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

class Employee:
    count = 0
    def __init__(self):
        Employee.count = Employee.count + 1


# creating objects
e1 = Employee()
e2 = Employee()
e3 = Employee()
print("The number of Employee:", Employee.count)
Code language: Python (python)

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

class Test:

    def __init__(self, i):
        self.id = i
        return True

d = Test(10)
Code language: Python (python)

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 object class.
  • The constructor must return None. Returning any other value raises a TypeError.
  • 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 argument self refers to the current object.
  • Constructor overloading isn’t supported in Python. Use default arguments or @classmethod alternative constructors instead.
  • A child class constructor can invoke the parent class constructor using super().__init__().

Filed Under: Python, Python Object-Oriented Programming (OOP)

Did you find this page helpful? Let others know about it. Sharing helps me continue to create free Python resources.

TweetF  sharein  shareP  Pin

About Vishal

I’m Vishal Hule, the Founder of PYnative.com. As a Python developer, I enjoy assisting students, developers, and learners. Follow me on Twitter.

Related Tutorial Topics:

Python Python Object-Oriented Programming (OOP)

All Coding Exercises:

C Exercises
C++ Exercises
Python Exercises
Java Exercises
C# Exercises

Python Exercises and Quizzes

Free coding exercises and quizzes cover Python basics, data structure, data analytics, and more.

  • 15+ Topic-specific Exercises and Quizzes
  • Each Exercise contains 25+ questions
  • Each Quiz contains 25 MCQ
Exercises
Quizzes
In: Python Python Object-Oriented Programming (OOP)
TweetF  sharein  shareP  Pin

  Python OOP

  • Python OOP
  • Classes and Objects in Python
  • Constructors in Python
  • Python Destructors
  • Encapsulation in Python
  • Polymorphism in Python
  • Inheritance in Python
  • Python Instance Variables
  • Python Instance Methods
  • Python Class Variables
  • Python Class Method
  • Python Static Method
  • Python Class Method vs. Static Method vs. Instance Method
  • Python OOP exercise

 Explore Python

  • Python Tutorials
  • Python Exercises
  • Python Quizzes
  • Python Interview Q&A
  • Python Programs

All Python Topics

Python Basics Python Exercises Python Quizzes Python Interview Python File Handling Python OOP Python Date and Time Python Random Python Regex Python Pandas Python Databases Python MySQL Python PostgreSQL Python SQLite Python JSON

About PYnative

PYnative.com is a platform built to help developers learn by doing. Solve coding exercises, take MCQs, and run code directly in built-in online compilers.

Follow Us

To get New Python Tutorials, Exercises, and Quizzes

  • Twitter
  • Facebook
  • Sitemap

Explore Python

  • Learn Python
  • Python Basics
  • Python Databases
  • Python Exercises
  • Python Quizzes
  • Online Python Code Editor
  • Python Tricks

Coding Exercises

  • C Exercises
  • C++ Exercises
  • Python Exercises
  • Java Exercises
  • C# Exercises

MCQ Quizzes

  • Python MCQs
  • Java MCQs
  • C++ MCQs

Legal Stuff

  • About Us
  • Contact Us

We use cookies to improve your experience. While using PYnative, you agree to have read and accepted our:

  • Terms Of Use
  • Privacy Policy
  • Cookie Policy

Copyright © 2018–2026 pynative.com