Python Reference ยท Built-in Functions
Python callable() function
Reference for Python callable(): check whether an object can be called like a function, which objects qualify, how __call__ works and what the check cannot promise.
The built-in callable() function returns True if you can call the object with parentheses, like obj(), and False otherwise. Functions, methods, lambdas, classes and instances of classes that define __call__() are all callable.
It is useful when an argument may be either a value or a function that produces the value, such as a default factory or a callback.
callable(len) # True callable("text") # False
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.
Syntax
callable(object)
| Name | Description |
|---|---|
object |
Any object. |
Return value: True if the object's type defines __call__(), otherwise False.
Raises: nothing.
Usage notes
- Classes are callable. Calling a class creates an instance, so
callable(Dog)isTrue. An instance is callable only if its class defines__call__(). - A true result is not a promise.
callable(x)says a call is possible, not that it works with your arguments. The call can still raiseTypeErrorfor wrong arguments, or any error the function raises. - A false result is reliable. If
callable()returnsFalse,x()raisesTypeError. - Instance attributes don't count. The check looks at the type, so setting
obj.__call__ = ...on an instance does not make it callable. - Decorators. Many decorators accept either a function or arguments;
callable()is a quick way to tell the two forms apart, though checking withinspector a sentinel is more robust. - Typing. For type hints use
collections.abc.Callable, for exampleCallable[[int], str]. - Not the same as
hasattr(x, "__call__"). Prefercallable(); it is clearer and checks the type.