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.
In Python, you can use the built-in callable() function to check if a variable is a function. Here is an example code snippet:
Check if a variable is a function in Python
def my_function():
pass
var1 = my_function
if callable(var1):
print("var1 is a function")
else:
print("var1 is not a function")
<div class="alert alert-info flex not-prose">![]()
<span class="hidden md:block">Watch a video course</span>Python - The Practical Guide</div>
Note that callable will return True for all callable objects, including classes, instances with call methods, and built-in functions.