Type Checking: callable(), type()

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 ...

Get Python Programming with the Java™ Class Libraries: A Tutorial for Building Web and Enterprise Applications with Jython now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.