__What's the difference between init and call?

Keywords: Python

I want to know the difference between init and call methods.

For example:

class test:

  def __init__(self):
    self.a = 10

  def __call__(self): 
    b = 20

#1 building

In Python, functions are the first class of objects, which means that function references can be passed to and executed from other functions and / or methods in input.

Instances of classes (also known as objects) can be treated as functions: passing them to other methods / functions and calling them. In order to achieve this, the function of the call class must be private.

Def call (self, [args...]) it takes a variable number of parameters as input. Assuming that x is an instance of class X, then X. "call" x(1,2) x (1,2) is similar to calling x(1,2) or the instance itself as a function.

In Python, "init" () "del" () is properly defined as a class constructor (and "del" () is a class destructor). Therefore, there is an obvious difference between "init" () and "call" (): the first one is to build an instance of Class up, and the second one is to make the instance call as a function without affecting the life cycle of the object itself (that is, the "call" does not affect the construction / destruction life cycle), but it can modify its internal state (as shown below).

Case.

class Stuff(object):

    def __init__(self, x, y, range):
        super(Stuff, self).__init__()
        self.x = x
        self.y = y
        self.range = range

    def __call__(self, x, y):
        self.x = x
        self.y = y
        print '__call__ with (%d,%d)' % (self.x, self.y)

    def __del__(self):
        del self.x
        del self.y
        del self.range

>>> s = Stuff(1, 2, 3)
>>> s.x
1
>>> s(7, 8)
__call__ with (7,8)
>>> s.x
7

#2 building

__call to make an instance of the class callable. Why is it needed?

Technically, an object is called once by new when it is created so that it can be init ialized.

But in many scenarios you may want to redefine your object, such as when you have finished the object, and you may need a new object. Use call to redefine the same object as if it were new.

It's just a case, and more.

#3 building

>>> class A:
...     def __init__(self):
...         print "From init ... "
... 
>>> a = A()
From init ... 
>>> a()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: A instance has no __call__ method
>>> 
>>> class B:
...     def __init__(self):
...         print "From init ... "
...     def __call__(self):
...         print "From call ... "
... 
>>> b = B()
From init ... 
>>> b()
From call ... 
>>> 

#4 building

We can use the call method to use other class methods as static methods.

class _Callable:
    def __init__(self, anycallable):
        self.__call__ = anycallable

class Model:

    def get_instance(conn, table_name):

        """ do something"""

    get_instance = _Callable(get_instance)

provs_fac = Model.get_instance(connection, "users")  

#5 building

I'll try to explain this with an example, assuming you want to print a certain number of terms from the Fibonacci series. Remember, the first two terms of the Fibonacci series are 1. For example: 1,1,3,3,5,8,13

You want the list containing Fibonacci numbers to be initialized only once and should be updated later. Now we can use the call function. Read @ edit Verma's answer. It's like you want the object to be called as a function, but it doesn't reinitialize when you call it.

For example:

class Recorder:
    def __init__(self):
        self._weights = []
        for i in range(0, 2):
            self._weights.append(1)
        print self._weights[-1]
        print self._weights[-2]
        print "no. above is from __init__"

    def __call__(self, t):
        self._weights = [self._weights[-1], self._weights[-1] + self._weights[-2]]
        print self._weights[-1]
        print "no. above is from __call__"

weight_recorder = Recorder()
for i in range(0, 10):
    weight_recorder(i)

The output is:

1
1
no. above is from __init__
2
no. above is from __call__
3
no. above is from __call__
5
no. above is from __call__
8
no. above is from __call__
13
no. above is from __call__
21
no. above is from __call__
34
no. above is from __call__
55
no. above is from __call__
89
no. above is from __call__
144
no. above is from __call__

If you observe that the output ﹐ init ﹐ is called only once, it is the first time that the class is instantiated, and then the object is called without reinitialization.

Posted by php_coder_dvo on Sat, 01 Feb 2020 03:05:02 -0800