Summary: In this tutorial, we will learn what __init__ and self are in Python and when we should use them in our Python program.

What is __init__ in Python?

__init__ is a reserved function in Python that is invoked when an object of a class is created (once per object, similar to the constructors in Java and C++).

It does not return any value and is mainly used to initialize the properties of an object.

For instance, consider the following Python class:

class Car:
    def __init__(self):
        print("Building the Car")
        self.name = "Lamborghini"
        self.max_speed = "220 mph"
        
    def display(self):
        print(f"Name: {self.name}")
        print(f"Max Speed: {self.max_speed}")
        
#creating object of the class
myCar = Car()

print("-----------------")

#calling the display function of the myCar object
myCar.display()

Output:

Building the Car
—————–
Name: Lamborghini
Max Speed: 220 mph

As we can see, during the creation of the object the __init__ method was automatically invoked. Inside the same, we have initialized the name and max_speed properties of the myCar object and output them into the display function.

Similarly, we can write other statements as well, that we want to execute at the time of the creation of the object.

Tip: Use __init__ function for the initialization of the data members.

__init__ function with Parameters

We can also pass arguments to the __init__ method to initialize data members of different objects with unique values.

For this, we declare parameters in the declaration of the__init__ function and pass the values for the same during the object creation as follows:

class Car:
    def __init__(self, name, speed):
        print("Building the Car")
        self.name = name
        self.max_speed = speed
        
    def display(self):
        print(f"Name: {self.name}")
        print(f"Max Speed: {self.max_speed}")
        

myCar = Car("Maruti", "120 mph")
print("-----------------")
myCar.display()

Output:

Building the Car
—————–
Name: Maruti
Max Speed: 120 mph

It is important to know that the value for self is passed by Python itself, we do not need to pass the value for it.

What is self in Python?

self is a keyword in Python that holds the reference of the current instance of the class.

It is used to access the properties and functions of the current working instance.

Here, the current instance means the object in action.

Python passes the reference of the current instance as the first parameter to the every instance methods of the class including the __init__ function, hence it is important to declare self as the first parameter in those functions to hold the reference of the object.

We have used the self keyword in the previous example to assign and access the values of the data members.

Consider the following code for instance:

class Car:
    def __init__():
        pass
        
    def display():
        pass
        
myCar = Car()
myCar.display()

Output:

: __init__() and display() takes 0 positional arguments but 1 was given

It is clear from the error, that Python by default is passing one argument to the __init__ and display methods, but they don’t have any parameter to hold the value.

Let’s catch the value in the self and output it to see what’s being passed:

class Car:
    def __init__(self):
        print(self)
        
    def display(self):
        print(self)
        
myCar = Car()
myCar.display()

Output:

<__main__.Car object at 0x7f65e4330d00>
<__main__.Car object at 0x7f65e4330d00>

From the output, we can observe that the value of self in both functions are the same instance of the Car class.

Can We Rename self?

Declaring the first parameter in the declaration of every instance method is important, however, it doesn’t have to be only self.

We can name the keyword to whatever we like.

class Car:
    def __init__(obj):
        print(obj)
        
    def display(car):
        print(car)
        
myCar = Car()
myCar.display()

Output:

<__main__.Car object at 0x7f65e4330d00>
<__main__.Car object at 0x7f65e4330d00>

Adarsh Kumar

I am an engineer by education and writer by passion. I started this blog to share my little programming wisdom with other programmers out there. Hope it helps you.

Leave a Reply