callable is an object in Python that can be called / executed when called with parantheses
( ). Classes and functions are callable.
Callables can be a class, a function, or an instance of a class. In simple terms, a class/function/instance/builtin is callable if it gets executed while being called with the parantheses
In : help() Welcome to Python 3.6's help utility! -- content omitted -- -------- In : int() Out: 0 In : callable(int) Out: True -------- In : callable(help) Out: True -------- In : def hello(): ..: print("Howdy!!") In : hello() Howdy!! In : callable(hello) Out: True
In Example 1, we can see that the builtins like
help(), a pre-defined type such as
int(), and a custom function
hello() are all callable. These can be executed while being called with parantheses.
The __call__() method
callable() builtin helps to determine if an object is callable or not. Internally, it translates to the magic method
my_object(*args) translates to
All classes and functions are callable, as well as instances of classes with the
__call__ magic method. An instance of a class/function is usually not callable (even though the class/function itself is), unless the class carries a
__call__ magic method.
In other words, an instance is callable only if the class it is instantiated from contains the
__call__ magic method.
- The inbuilt documentation on
In : print(callable.__doc__) Return whether the object is callable (i.e., some kind of function). Note that classes are callable, as are instances of classes with a __call__() method.
In : def hello(): ...: print("Howdy!!") In : hello() Howdy!! In : hello.__call__() Howdy!! In : callable(hello) Out: True
Example 2 shows that a function when called with the parantheses (including any required arguments) is equivalent to calling the
__call__() magic method. ie.. calling a function/class with parantheses translates to calling the
__call__() magic method.
Read more on Magic methods in Python
Example 3: Non-callable Instances
In : type(1) Out: int In : callable(1) Out: False In : x = 1 In : type(x) Out: int In : callable(int) Out: True In : callable(x) Out: False
Example 3 above shows that even though the
int class is callable, the instances created from the
int class are not.
Remember that instances will only be callable if the class from which it was instantiated contains a
__call__ method. Inspecting the methods of
class int reveals that it does not have a
NOTE: You can view the methods of the
int class using
Example 4: Another example with Classes
In : class tell: ...: def __call__(self): ...: pass In : telling = tell() In : callable(tell) Out: True In : callable(telling) Out: True -------- In : class test: ...: pass In : testing = test() In : callable(test) Out: True In : callable(testing) Out: False
Since all classes are by default callable, both the classes
test in Example 4 are callable. But the instances of these classes necessarily need not be so. Since the class
tell has the magic method
__call__, the instance
telling is callable. But the instance
testing instantiated from the class
test is not since the class does not have the magic method. Another set of examples.
Example 5: Non-callable instance of a class
In : class new: ...: def foo(self): ...: print("Hello") In : n = new() In : n() ------------------ TypeError Traceback (most recent call last) in module() ----> 1 n() TypeError: 'new' object is not callable
Example 6: Callable instance of the same class
In : class new: ...: def __call__(self): ...: print("I'm callable!") In : n = new() In : n Out: __main__.new at 0x7f7a614b1f98 In : n() I'm callable!
Example 5 and Example 6 shows how a class is itself callable, but unless it carries a
__call__() method, the instances spawned out of it are not so.