Python Glossary Русский

Python 3.2+Built-in functionsterm

callable

introspection

Returns True if the object can be called: a function, a class, a lambda or an object with a __call__ method.

Syntax

callable(object)

The check looks at the object's type, not the object: assigning obj.__call__ = f to an instance is not enough, because Python looks up __call__ on the class, so callable(obj) stays False and the call fails too. A True answer promises little either — the object is callable, but calling it with the wrong arguments still raises TypeError. In everyday code you rarely need the check: calling and handling TypeError, or branching on isinstance, is more direct; callable earns its keep at an API boundary where a value or a factory may arrive.

Examples

print(callable(print))    # → True
print(callable(len))      # → True
print(callable(42))       # → False
print(callable(lambda: 0))  # → True
class A:
    def __call__(self): pass
print(callable(A()))  # → True

See also

Open in the full glossarydocs.python.orgReport an inaccuracy