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.
The
callablebuilt-in mentioned in other answers doesn’t answer your question as posed, because it also returnsTrue, 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 theinspectmethod 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 theinspectmodule for introspection when it’s there, in preference to lower-level approaches:inspecthelps keep your code concise, clear, robust, and future-proof).