Callables
Python's definition of
callable object includes the obvious function definitions created with the def
statement.
It also includes, informally, any class with a __call__()
method. We can see several examples of this in Python 3 Object Oriented Programming, Dusty Phillips, Packt Publishing. For it to be more formal, we should make every callable class definition a proper subclass of collections.abc.Callable
.
When we look at any Python function, we see the following behavior:
>>> abs(3) 3 >>> isinstance(abs, collections.abc.Callable) True
The built-in abs()
function is a proper instance of collections.abc.Callable
. This is also true for the functions we define. The following is an example:
>>> def test(n): ... return n*n ... >>> isinstance(test, collections.abc.Callable) True
Every function reports itself as Callable
. This simplifies the inspection of an argument value and helps write meaningful debugging messages.
We'll take a look at callables in...