Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

What Makes an Object Callable in Python

Learn what makes an object callable in Python, how callable() works, and when implementing __call__ creates a useful callable object.

~3 min readPython
#python#callable()#__call__#functools.partial
Illustration of a Python object becoming callable through the __call__ method, shown as a function-style button with a state container.

In Python, any object whose type has a __call__ method in its method resolution order is callable, meaning you can invoke it with parentheses. The built-in callable() function checks that at runtime without invoking the object. Understanding the callable protocol helps you decide when to pass a plain function, a lambda, or a callable instance into an API.

What the callable() Check Actually Does

The callable() built-in returns True when the object's type has a __call__ method. It does not execute anything; it only inspects the type's method resolution order.

python
def greet(): return "hello" print(callable(greet)) # True print(callable(42)) # False print(callable("text")) # False

The check is based on the type, not the instance. If a class defines __call__, every instance of that class is callable, even if the instance itself has no call-related attributes.

Built-in Callables Beyond Functions

Functions are the most obvious callables, but several other built-in types are callable:

  • Classes: calling a class constructs an instance
  • Bound methods: they carry the instance and can be invoked directly
  • Lambda expressions: anonymous functions created at runtime
  • Generator functions: calling them returns a generator object
  • Built-in functions such as len or print
python
class Point: def __init__(self, x, y): self.x = x self.y = y p = Point(1, 2) # class is callable print(callable(Point)) # True

Because classes are callable, factory patterns and dependency injection work naturally in Python without extra wrapper functions.

Implementing call on a Class

You can make instances of your own classes callable by defining __call__. This is useful when an object must carry state or configuration while still behaving like a function.

python
class Counter: def __init__(self): self.count = 0 def __call__(self): self.count += 1 return self.count counter = Counter() print(counter()) # 1 print(counter()) # 2

The instance holds mutable state across calls, which a plain function typically handles with a closure or a global variable. The class-based version keeps the state explicit and can also expose helper methods. This pattern appears in retry counters, rate limiters, and memoization wrappers.

When a Callable Object Beats a Closure

Both closures and callable instances can capture state. The choice depends on whether the state is simple or whether you need additional methods, introspection, or inheritance.

python
def make_counter(): count = 0 def increment(): nonlocal count count += 1 return count return increment counter_fn = make_counter()

A closure is lightweight and sufficient for a single function. A callable class becomes the better choice when you need to expose the state, reset it, or combine several behaviors on the same object. For example, a callable that also provides a reset() method gives you control that a closure cannot offer cleanly.

functools.partial and Other Callable Factories

The functools module provides partial, which returns a new callable with some arguments pre-filled. The result is callable even though it is not a regular function.

python
from functools import partial def power(base, exponent): return base ** exponent square = partial(power, exponent=2) print(square(5)) # 25

partial objects expose attributes such as func, args, and keywords, so you can inspect what they wrap. This matters when you are debugging or inspecting callback configuration.

Performance and Overhead Considerations

Calling an instance with __call__ requires looking up the method on the type and then invoking it, while calling a plain function goes directly to the function object. In most code the extra lookup is negligible; if you are in a performance-critical loop, profile before replacing callable objects.

Common Mistakes with Callables

A frequent mistake is assigning __call__ to an instance instead of the class. The call mechanism looks up the method on the type, not on the instance dictionary, so the instance remains non-callable.

python
class Broken: pass obj = Broken() obj.__call__ = lambda: "nope" print(callable(obj)) # False

Another common error is checking callable() on a class rather than an instance when the intent is to test an instance's behavior. Remember that classes are always callable because calling a class constructs an instance.

Compatibility and Version Behavior

The callable() built-in has existed since early Python versions and behaves consistently in Python 3. The __call__ protocol is stable across Python 3 versions. In Python 3, classes and functions are first-class objects, so the callable protocol applies uniformly; there is no separate function-pointer concept as in C. Any callable can be passed where a callback is expected, which is why libraries commonly accept functions, lambdas, and callable instances interchangeably.

Try matching browser tools

All tools

Small utilities that fit Python work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Python guides

More in Python

All Python guides

The guides filed next to this one in Python, in library order.