June 2002
Beginner to intermediate
640 pages
14h 24m
English
The callable(object) function returns true if the object can be called like a function. Since more than object types can be callable (lambdas, functions, methods, class instances, etc.), this feature is very useful. Here's an example:
>>> callable(dir) 1
This is nice, but how do you use it? Let's say that you want to find out which objects in the current module are callable.
Define a class whose instances are callable.
>>> class myclass: ... def __call__(self): ... return "hello" ...
Define an instance of the callable class.
>>> hello = myclass()
Define some not so useful functions.
>>> def func():pass ... >>> def func1():pass ... >>> def func2():pass ... >>> def func3():pass
Iterate through the list ...
Read now
Unlock full access