Difference between __init__ , __call__ and __new__?
This tutorial is for What is the difference between __init__, __call__ and __new__? Python is the built-in method having two prefixes and suffix underscores in the method name. Dunder means Double Underscores. magic methods are : __init__, __add__, __new__, __call__,__len__, __repr__ etc. In this article, we are going to see the difference between __init__, __call__, and __new__.
__init__()
__init__() method in the python constructor is invoked automatically when an object of the class is defined. 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, number): print("__init__() call") self.data = numberdef __str__(self):
print("__str__() call")
print("Number is {}".format(self.data))
# Class A declaration x = A(16) x.__str__() # Declaration of another instance y = A(20) y.__str__()
Output
__init__() call __str__() call Number is 16 __init__() call __str__() call Number is 20
__call__()
__call__() we need to understand what a callable object is. A callable object is one that can be called a function. __call__() is used to resolve the code associated with a callable object. __call__() doesn’t make the object not work like a normal one. One thing to keep in mind is the object is itself used as a function.
Example
class A: def __init__(self, number): print("__init__() call") self.data = number def __str__(self): print("__str__() call") print("Number is {}".format(self.data))def __call__(self):
num = 0
print("__call__() call")
print("Adding 10 to the value of data")
num = self.data + 10
return num
x = A(16) x.__str__() y = x() print(y) # Declaration of another instance of class A z = A(23) z.__str__() # __call__() calling for z return_val = z() print(return_val)
Output
__init__() call
__str__() call
Number is 16
__call__() call
Adding 10 to the value of data
26
__init__() call
__str__() call
Number is 23
__call__() call
Adding 10 to the value of data
33
__new__()
__new__ method will be called when an object is created. __init__ method will be called to initialize the object. __new__ method is defined as a static method that requires passing a parameter argument1.
Example
class Devnote(object):
def __str__(self):
return "Devnote"
class Dev(object):
def __new__(argument1):
return Devnote()
def __init__(self):
print ("init call")
print(Dev())
Output
Devnote
__INIT__() | __CALL__() | __NEW__() |
---|---|---|
__INIT__() is same as a constructor, it initializes the values. | __CALL__() is used for a direct call using the object. | __NEW__() is used object is created and __init__ method will be called to initialize the object. |
Invoked automatically when an object is declared and called by an regular object. | Invoked automatically by a callable and called by a callable object. | Invoked automatically defined as a static method which requires to pass a parameter argument1 |
Example: x = A(16) # statement 1 x() # statement 2 __init__() is called statement 1 |
Example: x = A(16) # statement 1 x() # statement 2 __call__() is called statement 2 |
Example: x = A() # statement 1 y = B() # statement 2 y() # statement 3 __new__() is called statement 3 |