sam sam - 2 years ago 105
Python Question

What is the difference between __init__ and __call__ in Python?

I want to know what the difference between

methods is.

For example:

class test:

def __init__(self):
self.a = 10

def __call__(self):
b = 20

Answer Source

The first is used to initialise newly created object, and receives arguments used to do that:

class foo:
    def __init__(self, a, b, c):
        # ...

x = foo(1, 2, 3) # __init__

The second implements function call operator.

class foo:
    def __call__(self, a, b, c):
        # ...

x = foo()
x(1, 2, 3) # __call__
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download