0 votes
1 view
in Python by (15.7k points)

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 Answer

0 votes
by (107k points)

The __init__   method is used to initialise the newly created object, and it receives arguments that are used to initialize the newly created object.

class Foo:

    def __init__(self, a, b, c):

        x = Foo(1, 2, 3) # This is the __init__ method.

The call() method is used to implement the function call operator.

class Foo:

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

          x = Foo()

          x(1, 2, 3) #This is the call() method.

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...