Python Class __call__() Method

Python class provides the __call__() Butlin method in order to use an instance or object like a function. This may seem strange as an object is different in function. Even we can provide some parameters for this instance call.

__call__() Method

We can define the __call__() method inside the class similar to the __init__() method. The __call__() method is a private method that can not be called via an object or instance. We can also provide self access to the instance variables and methods.

class Person:
   def __call__(self):
      print("This is the __call__() method")

p = Person()

p()

__call__() Method with Parameters

We can use the __call__() method with parameters to make things more dynamic. In the following example, we define 2 parameters for the __call__() method.

class Person:
   def __call__(self,name,surname):
      print("My name is ",name," ",surname)

p = Person()

p("İsmail","Baydan")

Leave a Comment