You are currently viewing __init__ and __call__ In Python – What Do They Do?

__init__ and __call__ In Python – What Do They Do?

You may have encountered the methods in Python that are prefixed and suffixed with double underscores, those methods are called “Dunder Methods”. These methods are also called “magic methods”.

Dunder methods are used to overload specific methods in order to make their behavior unique to that class.

Python has a rich collection of built-in dunder methods and a few are listed below:

  • __init__
  • __call__
  • __repr__
  • __str__
  • __len__
  • and more…

In this article, we will look at two dunder methods(__init__ and __call__) that are commonly used in Python classes. How do they differ, and when do we need to use them within the Python program?

Watch Video

__init__ Method

The __init__ method is used to initialize the object’s attributes after the object of the class is created.

The __init__ for the class Language is created in the preceding code, and it takes the parameters lang and year.

Then we created the instance of the class Language, passed it the necessary arguments, and called the method data on the instance object.

The arguments "Python" and "1991" passed to the class are actually stored in the parameters lang and year passed to the __init__ method, which initializes the instance variables (self.lang and self.year) with these values.

Every time we create an object, the __init__ method is automatically invoked. We’ll get different results if we create another object and pass different values.

init visualization

Syntax

Thus, we can conclude that the syntax of the __init__ method can be written as the following.

Here,

self – is an instance of the class. Mandatory.

arg1 and arg2 – are the parameters. We can pass as many parameters as we want or the field can also be left empty.

What if we pass more than the number of parameters that a class takes?

The above code will throw an error and prompts the following message.

The message states that two arguments were allowed, but three were passed. But we passed two arguments, not three then why did this happen?

This occurred because __init__ only accepts self and lang. When we instantiated the class with arguments, the keyword self, which represents the object’s instance, was passed along with the arguments automatically.

So, when we passed the arguments "JavaScript" and "1995", the self was automatically passed, making three arguments passed to the class Language.

__init__ with and without parameters

Python constructor(__init__) can be created with or without passing any parameters.

Default __init__ constructor

A constructor created without parameters other than self (a reference to the instance being constructed) is called the default constructor.

We created self.lang and self.year and assigned the default values "C++" and "1985" respectively. We accessed the lang and year by using the instance of the class object1.

We can also override the attribute’s default value by assigning a new value before accessing the attribute from the class.

__init__ with parameters

We’ve already seen some examples where we used parameters to create the constructor. Pass the parameters to the constructor, as shown in the following example. Then we created an object or instance of the Vehicle class and passed the arguments to it. The output was then obtained by calling the vehicle function.

__call__ method

When we invoke a function, we simply use the function name with parenthesis, such as hello(), to notify the interpreter that a function is being called. Well, we can say that it is a shorthand for hello.__call__().

When we invoke a function in Python, the interpreter executes the __call__ method in the background.

In the above code, first, we called the function func simply as we usually do and then by using the __call__.

__call__ inside Python classes

The concept behind using __call__ is to call the instances of the classes as if they were a function. Instances of classes can be made callable by defining a __call__ method in their class.

In this case, we called the class object example as if it were a function.

Syntax

The syntax of the __call__ method is

object.__call__(self, *args, **kwargs)

Here,

self – reference of the object.

args and kwargs – arguments and keyword arguments.

__call__ with parameters

We passed the parameter school to the __call__ method just like we do when we create the constructor. This will allow us to pass the argument within the object of the class as we did in the above code.

__call__ with decorators

We first generated the decorator(@Demo) for the class Demo, followed by the function decor. Then we invoked the decor function and got the result shown below.

The decorator altered the behaviour of our class Demo, and we accessed the class’s attributes simply by invoking the decor function.

If we examine more closely, the function decor was supplied as an argument to the class Demo, the decor’s return value was saved within the action variable, and the class was called and produced the output.

The decorator part in the above code is equivalent to the following.

Conclusion

The __init__ method is also called the constructor method which is used to initialize the objects of the specific class whereas the __call__ method allows us to call the object of the class like a function.

The __init__ method created without passing parameters is called the default __init__ constructor.

When we call a function using (), in actuality, the __call__ method is implemented in the function.

We’ve coded many examples to get a better understanding of both methods.


🏆Other articles you might be interested in if you liked this one

Introduction to the different types of class inheritance in Python.

Build a deep learning model using the transfer learning technique.

Tackle the shortage of data by using the data augmentation technique.

Upload and display images on the frontend using Flask in Python.

Integrate TailwindCSS into the Flask app.

Avoid conflicts between dependencies by using virtual environments.


That’s all for now

Keep Coding✌✌