Skip to content

User-Defined Callable Types

In Python, we can make arbitrary objects behave like functions by implementing a __call__ instance method. This simple approach allows us to create function-like objects with internal state that persists across invocations. By defining the __call__ method, we enable instances of the class to be callable, just like regular functions.

user_callable_object.py
>>> import random
>>> class BingoCage:
>>>     def __init__(self, items):
...        self._items = list(items)  
...        random.shuffle(self._items)  
>>>     def pick(self):
...        try:
...            return self._items.pop()
...        except IndexError:
...            raise LookupError('pick from empty BingoCage')  
>>>     def __call__(self):
...        return self.pick()
>>> bingo = BingoCage(range(3))
>>> print(bingo.pick())
>>> print(bingo())
>>> print(callable(bingo))
0
1
True

References