ZetCode

Python callable Function

Last modified April 11, 2025

This comprehensive guide explores Python's callable function, which checks if an object appears callable. We'll cover functions, methods, classes, and practical examples of determining callability.

Basic Definitions

The callable function returns True if the object appears callable (can be called like a function). It returns False otherwise.

Key characteristics: works with functions, methods, classes, and objects implementing __call__. It helps determine if parentheses syntax would work on an object.

Basic Function Check

Here's simple usage showing how callable identifies different callable objects in Python.

basic_callable.py
def my_function():
    return "Hello"

print(callable(my_function))  # True
print(callable(print))       # True (built-in function)
print(callable("hello"))     # False (string)
print(callable(42))          # False (integer)

This example shows callable with different object types. Functions return True, while non-callable objects like strings and numbers return False.

The function helps determine if you can invoke an object with parentheses syntax. It's useful for dynamic programming scenarios.

Checking Class Callability

Classes are callable (they create instances), while instances may or may not be callable. This example demonstrates the difference.

class_callable.py
class MyClass:
    pass

class CallableClass:
    def __call__(self):
        return "Called!"

obj1 = MyClass()
obj2 = CallableClass()

print(callable(MyClass))      # True (classes are callable)
print(callable(obj1))         # False (no __call__ method)
print(callable(obj2))         # True (implements __call__)

The example shows classes are always callable (they can instantiate objects). Instances are only callable if they implement the __call__ method.

This distinction is important when working with object-oriented Python code and dynamic instantiation.

Methods and Lambdas

This example demonstrates callable with methods and lambda functions, which are also callable objects.

methods_lambdas.py
class Calculator:
    def add(self, a, b):
        return a + b

calc = Calculator()
lambda_func = lambda x: x * 2

print(callable(Calculator.add))  # True (unbound method)
print(callable(calc.add))        # True (bound method)
print(callable(lambda_func))     # True (lambda)
print(callable(str.upper))      # True (string method)

Methods (both bound and unbound) and lambda functions are callable. The callable function correctly identifies all these cases.

This behavior is consistent across different types of callable objects in Python's object model.

Custom Callable Objects

You can make objects callable by implementing the __call__ method. This example creates a callable counter object.

custom_callable.py
class Counter:
    def __init__(self):
        self.count = 0
    
    def __call__(self):
        self.count += 1
        return self.count

counter = Counter()
print(callable(counter))  # True
print(counter())          # 1
print(counter())          # 2

The Counter class implements __call__, making instances callable. callable correctly identifies this capability.

This pattern is useful for creating function-like objects that maintain state between calls.

Practical Use Case

This example shows a practical use of callable in a plugin system where you need to verify callable handlers.

plugin_system.py
def validate_handlers(handlers):
    for name, handler in handlers.items():
        if not callable(handler):
            raise ValueError(f"Handler '{name}' is not callable")

handlers = {
    'greet': lambda: print("Hello"),
    'exit': "not_a_function",
    'calculate': sum
}

try:
    validate_handlers(handlers)
except ValueError as e:
    print(f"Validation error: {e}")

The code validates that all handlers in a dictionary are callable. It raises an error for non-callable entries (like the string "not_a_function").

This demonstrates how callable can be used for runtime validation in systems that expect callable objects.

Best Practices

Source References

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Python tutorials.