What is the difference between __init__ and __call__?

21 Apr, 2020
Dunder or magic methods in Python are the methods having two prefixes and suffix underscores in the method name. Dunder here means “Double Under (Underscores)”. These are commonly used for operator overloading. Few examples for magic methods are: __init__, __add__, __len__, __repr__ etc. In this article, we are going to see the difference between two such methods.
Note: For more information, refer to Dunder or magic methods in Python
This Python method is similar to a constructor in any other programming language. A constructor is a definition with the same name as the class and is invoked automatically when the object of that class is defined. A constructor initializes all the required entities of the program to make it more reliable.Similar to this definition __init__() works as a Python constructor it is invoked automatically when an object of the class is defined. It initializes the required members with default values provided. It can also be invoked with the values provided during the time of declaration of the object of the class.
EXAMPLE:
class A:    def __init__(self, x):        print("inside __init__()")        self.y = x      def __str__(self):        print("inside __str__()")        print("value of y:", str(self.y))  # declaration of instance of class Aa = A(3)  # calling __str__() for object aa.__str__()  # declaration of another instance # of class Ab = A(10)  # calling __str__() for bb.__str__()
OUTPUT:
inside __init__()
inside __str__()
('value of y:', '3')
inside __init__()
inside __str__()
('value of y:', '10')

Before getting into application of __call__() we need to understand what a callable object is.A callable object is one which can be called like a function.In Python, __call__() is used to resolve the code associated with a callable object. Any object can be converted to a callable object just by writing it in a function call format. An object of that kind invokes the __call__() method and executes the code associated with it. This doesn’t make the object not to work like a normal one. The object can be used as a normal is used.One thing to keep in mind is the object is itself used as a function, so syntax should be right.
EXAMPLE:
class A:    def __init__(self, x):        print("inside __init__()")        self.y = x      def __str__(self):        print("inside __str__()")        print("value of y:", str(self.y))      def __call__(self):        res = 0        print("inside __call__()")        print("adding 2 to the value of y")        res = self.y + 2        return res                  # declaration of instance of class Aa = A(3)  # calling __str__() for aa.__str__()  # calling __call__() for a r = a()print(r)  # declaration of another instance# of class Ab = A(10)  # calling __str__() for bb.__str__()  # calling __call__() for br = b()print(r)
OUTPUT:
inside __init__()
inside __str__()
('value of y:', '3')
inside __call__()
adding 2 to the value of y
5
inside __init__()
inside __str__()
('value of y:', '10')
inside __call__()
adding 2 to the value of y
12
python-oop-concepts
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Python Classes and Objects
Python OOPs Concepts
Introduction To PYTHON
How to drop one or multiple columns in Pandas Dataframe
Python | os.path.join() method
Check if element exists in list in Python
How To Convert Python Dictionary To JSON?
Python | Get unique values from a list
Create a directory in Python