How do I detect whether a variable is a function?
In Python, you can use the built-in callable()
function to check if a variable is a function. Here is an example code snippet:
def my_function():
pass
var1 = my_function
if callable(var1):
print("var1 is a function")
else:
print("var1 is not a function")
Watch a video course
Python - The Practical Guide
Note that callable will return True for all callable objects, including classes, instances with call methods, and built-in functions.