How to determine if the variable is a function in Python?

function, python

Solution

The `callable` built-in mentioned in other answers doesn't answer your question as posed, because it also returns `True`, besides functions, for methods, classes, instances of classes which define a `__call__` method. If your question's title and text are wrong, and you don't care if something is in fact a function but only if it's callable, then use that builtin. But the best answer to your question as posed is: import the `inspect` method of Python's standard library, and use inspect.isfunction. (There are other, lower-abstraction ways, but it's always a good idea to use functionality of the `inspect` module for introspection when it's there, in preference to lower-level approaches: `inspect` helps keep your code concise, clear, robust, and future-proof).

Problem

Since functions are values in Python, how do I determine if the variable is a function? For example: ``` boda = len # boda is the length function now if ?is_var_function(boda)?: print "Boda is a function!" else: print "Boda is not a function!" ``` Here hypothetical `?is_var_function(x)?` should return true if `x` is a callable function, and false if it is not.

Original source

Related problems