Skip to content

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:

Check if a variable is a function in Python

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"> Watch a course 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.

Do you find this helpful?

Dual-run preview โ€” compare with live Symfony routes.