How to Check if a Variable is a Function in Python
In Python, functions are first-class objects. This means you can assign them to variables, pass them as arguments, and return them from other functions. Sometimes, you need to check if a variable actually refers to a function (or another callable object).
This guide explains the best way to determine if a variable is a function in Python, using the built-in callable() function, and discusses alternative (but less comprehensive) approaches using inspect.isfunction(), types.FunctionType, and hasattr().
Using callable() (Recommended)
The built-in callable() function is the most reliable and Pythonic way to check if a variable refers to a function (or any other callable object):
def do_math(a, b):
return a + b
print(callable(do_math)) # Output: True
print(callable('hello')) # Output: False
if callable(do_math):
print('The variable is a function') # This will be printed
else:
print('The variable is NOT a function')
callable(object): ReturnsTrueif theobjectappears callable (i.e., it can be called like a function using parentheses()), andFalseotherwise.- It also returns
Truefor classes, as they are also callable. - The
callable()returnsTrueeven for built-in functions written in C.
Using inspect.isfunction() (Limited to Python Functions)
The inspect.isfunction() function from the inspect module checks if an object is a Python function (defined using def). It does not consider built-in functions or other callable objects to be functions.
import inspect
def do_math(a, b):
return a + b
print(inspect.isfunction(do_math)) # Output: True
class Employee:
pass
print(inspect.isfunction(Employee)) # Output: False (Even though classes are callable)
print(inspect.isfunction(zip)) # Output: False (Built-in function)
print(callable(zip)) # Output: True
inspect.isfunction()is very specific to Python function objects. It's less general thancallable().
Using types.FunctionType (Limited and Less Readable)
You might see code using types.FunctionType and isinstance():
import types
def do_math(a, b):
return a + b
print(isinstance(do_math, types.FunctionType)) # Output: True
print(isinstance(zip, types.FunctionType)) # Output: False (built-in function)
- This works similarly to
inspect.isfunction(). It's less direct and clear than usingcallable().
Using hasattr(__call__) (Checks for Callability, Not Function Type)
The __call__ method is what makes an object callable. You can check for its existence using hasattr():
def do_math(a, b):
return a + b
if hasattr(do_math, '__call__'):
print('The variable is a function') # Output: The variable is a function
else:
print('The variable is NOT a function')
print(hasattr(do_math, '__call__')) # Output: True
print(hasattr('tutorialreference.com', '__call__')) # Output: False
hasattr(object, '__call__'): Checks if the object has a__call__method. If it does, the object is callable.- However, this is essentially what
callable()does internally. Usingcallable()is more readable and directly expresses your intent.