The Built-In callable()
Function
by Christoph Schiessl on Python
Another useful and interesting built-in function is callable(object)
, which the official documentation explains as follows:
Return
True
if the object argument appears callable,False
if not. If this returnsTrue
, it is still possible that a call fails, but if it isFalse
, calling object will never succeed. Note that classes are callable (calling a class returns a new instance); instances are callable if their class has a__call__()
method.
This sounds easy enough, so let's try it out:
>>> def custom_function():
... pass
...
>>> callable(custom_function)
True
>>> custom_lambda = lambda: None
>>> callable(custom_lambda)
True
Ordinary functions and lambda
s are obviously callable. Everything else wouldn't make much sense, would it?
>>> callable(list)
True
>>> class CustomClass:
... pass
...
>>> callable(CustomClass)
True
Both, built-in classes (e.g., list
) and user-defined classes (e.g., CustomClass
), are also callable. As a matter of fact, classes are always callable, and if you do call them, you get a new instance of the class as the return value.
>>> callable(list())
False
>>> callable([])
False
>>> class CustomClass:
... pass
...
>>> callable(CustomClass)
True
Objects — aka instances of classes — are a different story. For example, instances of list
are not callable. By the way, list()
and []
are the same thing; both create a new instance of the list
class, and in both cases, the newly created list object doesn't contain elements. In other words, the two new lists are empty.
>>> class CustomClass:
... pass
...
>>> callable(CustomClass)
True
>>> callable(CustomClass())
False
>>> class CustomCallableClass:
... def __call__(self):
... pass
...
>>> callable(CustomCallableClass)
True
>>> callable(CustomCallableClass())
True
User-defined classes are not callable by default unless they define the dunder method __call__()
, which is what the CustomCallableClass
in the example above is doing.
You can also understand callable objects as follows: An object x = SomeClass()
can be called as x()
if and only if it is callable(x) == True
. If you call an object like this, what's happening behind the scenes is roughly equivalent to type(x).__call__(x)
. This works because type(x) == SomeClass
.
>>> class SomeClass:
... def __call__(self): # Add parameters if you need to ...
... print(f"{type(self).__name__}.__call__() has been called!")
...
>>> x = SomeClass()
>>> x()
SomeClass.__call__() has been called!
>>> type(x).__call__(x)
SomeClass.__call__() has been called!
Isn't this all fascinating? The point of this article was to demonstrate that everything in Python is potentially callable. Thank you for reading, and as always, please don't hesitate to reach out if you have any comments or questions.