Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages

Python Class Constructor – Python __init__() Function

Python class constructor function job is to initialize the instance of the class. Python __init__() is the constructor function for the classes in Python.


Python __init__() Function Syntax

The __init__() function syntax is:

def __init__(self, [arguments])
  • The def keyword is used to define it because it’s a function.
  • The first argument refers to the current object. It binds the instance to the init() method. It’s usually named “self” to follow the naming convention. You can read more about it at Python self variable.
  • The init() method arguments are optional. We can define a constructor with any number of arguments.

Python Class Constructor Examples

Let’s look at some examples of the constructor function in different scenarios.

1. Class with No Constructor

We can create a class without any constructor definition. In this case, the superclass constructor is called to initialize the instance of the class. The object class is the base of all the classes in Python.

class Data:
    pass


d = Data()
print(type(d))  # <class '__main__.Data'>

Here is another example to confirm that the superclass constructor is called to initialize the instance of the subclass.

class BaseData:

    def __init__(self, i):
        print(f'BaseData Constructor with argument {i}')
        self.id = i


class Data(BaseData):
    pass


d = Data(10)
print(type(d))

Output:

BaseData Constructor with argument 10
<class '__main__.Data'>

2. Simple Constructor with No-Arguments

We can create a constructor without any arguments. It’s useful for logging purposes such as keeping a count of the instances of the class.

class Data1:
    count = 0

    def __init__(self):
        print('Data1 Constructor')
        Data1.count += 1


d1 = Data1()
d2 = Data1()
print("Data1 Object Count =", Data1.count)

Output:

Data1 Constructor
Data1 Constructor
Data1 Object Count = 2

3. Class Constructor with Arguments

Most of the time, you will find the constructor function with some arguments. These arguments are generally used to initialize the instance variables.

class Data2:

    def __init__(self, i, n):
        print('Data2 Constructor')
        self.id = i
        self.name = n


d2 = Data2(10, 'Secret')
print(f'Data ID is {d2.id} and Name is {d2.name}')

Output:

Data2 Constructor
Data ID is 10 and Name is Secret

4. Class Constructor with Inheritance

class Person:

    def __init__(self, n):
        print('Person Constructor')
        self.name = n


class Employee(Person):

    def __init__(self, i, n):
        print('Employee Constructor')
        super().__init__(n)  # same as Person.__init__(self, n)
        self.id = i


emp = Employee(99, 'Pankaj')
print(f'Employee ID is {emp.id} and Name is {emp.name}')

Output:

Employee Constructor
Person Constructor
Employee ID is 99 and Name is Pankaj
  • It is our responsibility to call the superclass constructor.
  • We can use the super() function to call the superclass constructor function.
  • We can also use the superclass name to call its init() method.

5. Constructor Chaining with Multilevel Inheritance

class A:

    def __init__(self, a):
        print('A Constructor')
        self.var_a = a


class B(A):

    def __init__(self, a, b):
        super().__init__(a)
        print('B Constructor')
        self.var_b = b


class C(B):

    def __init__(self, a, b, c):
        super().__init__(a, b)
        print('C Constructor')
        self.var_c = c


c_obj = C(1, 2, 3)
print(f'c_obj var_a={c_obj.var_a}, var_b={c_obj.var_b}, var_c={c_obj.var_c}')

Output:

A Constructor
B Constructor
C Constructor
c_obj var_a=1, var_b=2, var_c=3

6. Constructor with Multiple Inheritance

We can’t use super() to access all the superclasses in case of multiple inheritances. The better approach would be to call the constructor function of the superclasses using their class name.

class A1:
    def __init__(self, a1):
        print('A1 Constructor')
        self.var_a1 = a1


class B1:
    def __init__(self, b1):
        print('B1 Constructor')
        self.var_b1 = b1


class C1(A1, B1):
    def __init__(self, a1, b1, c1):
        print('C1 Constructor')
        A1.__init__(self, a1)
        B1.__init__(self, b1)
        self.var_c1 = c1


c_obj = C1(1, 2, 3)
print(f'c_obj var_a={c_obj.var_a1}, var_b={c_obj.var_b1}, var_c={c_obj.var_c1}')

Output:

C1 Constructor
A1 Constructor
B1 Constructor
c_obj var_a=1, var_b=2, var_c=3

Python Doesn’t Support Multiple Constructors

Python doesn’t support multiple constructors, unlike other popular object-oriented programming languages such as Java.

We can define multiple __init__() methods but the last one will override the earlier definitions.

class D:

    def __init__(self, x):
        print(f'Constructor 1 with argument {x}')

    # this will overwrite the above constructor definition
    def __init__(self, x, y):
        print(f'Constructor 1 with arguments {x}, {y}')


d1 = D(10, 20) # Constructor 1 with arguments 10, 20

Can Python __init__() function return something?

If we try to return a non-None value from the __init__() function, it will raise TypeError.

class Data:

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

d = Data(10)

Output:

TypeError: __init__() should return None, not 'bool'

If we change the return statement to return None then the code will work without any exception.


References: