__call__Easy Examples

Makes an instance callable like a function (obj())

Implementing __call__

Basic implementation of __call__ in a class.

python
class Multiplier:
    def __init__(self, factor):
        self.factor = factor

    def __call__(self, value):
        return value * self.factor

double = Multiplier(2)
triple = Multiplier(3)
print(double(5))
print(triple(5))

__call__ makes an instance callable like a function (obj()). Implementing it lets you customize how Python interacts with your objects.

__call__ in action

Seeing __call__ called by Python's built-in operations.

python
# How Python calls __call__ automatically
class Demo:
    def __init__(self, value):
        self.value = value

    def __call__(self):
        print(f"__call__ was called!")
        return self

d = Demo(42)
# This triggers __call__:
d()

Python automatically calls __call__ when you use the corresponding operator or function on your object.

Want to try these examples interactively?

Open Easy Playground