In Python, functions are first-class objects, meaning they can be passed around and used just like any other object. Similarly, objects of user-defined classes can also behave like functions if they implement the __call__
method.
The __call__
method allows an instance of a class to be called as if it were a function. This is useful when you want objects to have the flexibility of functions but still carry state or logic encapsulated within a class.
class Multiplier:
def __init__(self, factor: int):
self.factor = factor
def __call__(self, value: int) -> int:
return self.factor * value
# Example usage:
double = Multiplier(2)
triple = Multiplier(3)
print(double(10)) # Outputs: 20
print(triple(10)) # Outputs: 30
In this example, the Multiplier
class is defined with an __init__
method that takes a factor
as a parameter. The factor
is stored as an instance attribute. The __call__
method is also implemented, allowing instances of Multiplier
to be called like functions. The __call__
method takes an integer argument value
and returns the product of value
and the factor
.
When an instance of the Multiplier
class is created, such as double = Multiplier(2)
, this object stores factor = 2
. Now, calling double(10)
is equivalent to executing the __call__
method, where the value
argument is set to 10
. The result is 2 * 10 = 20
. Similarly, for triple = Multiplier(3)
, calling triple(10)
results in 3 * 10 = 30
.
This makes the class instances behave like functions, while allowing them to maintain internal state (in this case, the factor
). The __call__
method is an elegant way to give objects the behavior of a function, making them highly flexible and reusable in different contexts.