Python Callable Objects and __call__
Learn how Python instances become callable with __call__, including state, arguments, return values, and inheritance.
Advanced
Python objects can be made callable by defining `__call__()`. That gives an instance function-like call syntax while allowing the object to carry state, configuration, and other behavior.
Make an instance callable
When an instance is followed by parentheses, Python can call its `__call__()` method. The method receives the instance as `self`, just like another instance method.
|
|
Defining `__call__()` makes a `Greeter` instance callable. The expression `greet("Mina")` invokes the method and returns its result.
Use normal argument rules
`__call__()` is a method, so its parameter list follows the normal function and method calling rules you already learned.
|
|
A callable instance can use ordinary function parameter features, including positional-only and keyword-only parameters.
Keep state between calls
A callable instance can store state on `self`. This is a major reason to choose a callable object over a plain function when the operation has persistent configuration or state.
|
|
A callable object can keep state on itself, so separate calls can observe and update the same attribute.
Check whether an object is callable
The built-in `callable()` returns `True` when its argument is callable and `False` otherwise.
|
|
The built-in `callable()` reports whether an object is callable. Functions and objects with `__call__()` return `True` here.
Return values normally
`__call__()` can return any value, just like another method. The surrounding call expression receives that returned value.
|
|
The value of `obj(...)` is the value returned by `__call__()`, unless the call raises an exception.
Give each object independent configuration
Each callable instance can carry different state or configuration while sharing the same implementation.
|
|
Separate instances can carry different configuration while sharing the same callable implementation.
Use inheritance normally
`__call__()` participates in ordinary method inheritance. A subclass can override it, inherit it, or extend a base implementation with `super()`.
|
|
`__call__()` is a normal special method for inheritance. A subclass can override it and use `super()` to continue with inherited behavior.
Know when a named method is clearer
Making an object callable is not automatically better. Use it when the object naturally represents one primary operation. When an object exposes several distinct actions, named methods can make the interface clearer.
|
|
A callable instance is useful when the object itself represents one primary operation. Named methods can be clearer when an object exposes several distinct actions.
A callable object is still an ordinary object: it can have attributes, other methods, inheritance, and state. Call syntax is simply an additional interface supplied by `__call__()`.
Key takeaways
- Defining __call__() makes instances of the class callable with function-like syntax.
- The __call__() method follows ordinary method argument and return-value rules.
- Callable objects can combine one primary operation with persistent state or configuration.
- callable() can report whether an object is callable; an instance with __call__() is callable.
- __call__() participates in normal inheritance, so subclasses can override or extend call behavior.
Thanks for your feedback.
Mark this learning resource complete to track your learning progress.