callable

In Python, a callable is any object that you can call like a function. This means you can use parentheses () to invoke the object and possibly pass arguments to it.

Typically, functions and methods are callable. However, other objects can be made callable by implementing the __call__() special method. This method allows you to create objects that behave like functions.

You can check if an object is callable using the built-in callable() function, which returns True if the object can be called and False otherwise.

Understanding the concept of callables is essential when working with higher-order functions, decorators, and any Python feature that requires passing around functions or function-like objects.

Example

Here’s an example demonstrating a class with the .__call__() method, making its instances callable:

Python
class Adder:
    def __init__(self):
        self.value = 0

    def __call__(self, value):
        self.value += value
        return self.value

# Usage
adder = Adder()
adder(10)  # Output: 10
adder(10)  # Output: 20
adder(10)  # Output: 30

In this example, Adder is a class whose instances are callable. They add any passed value to the .value attribute.

Tutorial

Python's .__call__() Method: Creating Callable Instances

In this tutorial, you'll learn what a callable is in Python and how to create callable instances using the .__call__() special method in your custom classes. You'll also code several examples of practical use cases for callable instances in Python.

intermediate python

For additional information on related topics, take a look at the following resources:


By Leodanis Pozo Ramos • Updated Jan. 20, 2025 • Reviewed by Dan Bader