Search notes:

Python: callable objects

An object becomes callable if it implements the __call__ method. It then behaves similar to a function:
class multiplier:

     def __init__(self, t):
         self.t = t

     def __call__(self, v):
         return self.t * v


mult_by_4 = multiplier(4)
mult_by_9 = multiplier(9)

print(mult_by_4(5)) # 20
print(mult_by_9(3)) # 27
Github repository about-Python, path: /objects/callable/demo.py

See also

The functools module.
Callable types

Index