Last updated: Apr 10, 2024
Reading timeยท3 min
Use the callable()
function to check if a variable is a function, e.g.
if callable(function):
.
The callable()
function takes an object and returns True
if the object is
callable, otherwise, False
is returned.
def do_math(a, b): return a + b print(callable(do_math)) # ๐๏ธ True print(callable('hello')) # ๐๏ธ False if callable(do_math): # ๐๏ธ this runs print('The variable is a function') else: print('The variable is NOT a function')
We used the callable()
built-in function to check if a variable is a function.
The callable
function takes an object as an argument and returns True
if the object appears
callable, otherwise False
is returned.
print(callable(lambda x: x * 2)) # ๐๏ธ True print(callable(3.14)) # ๐๏ธ False
If the callable()
function returns True
, it is still possible that calling
the object fails, however, if it returns False
, calling the object will never
succeed.
callable()
function doesn't check if the passed-in value is specifically a function, it checks if the value is callable.It also returns True
for classes.
class Employee: pass print(callable(Employee)) # ๐๏ธ True print(callable(int)) # ๐๏ธ True
An alternative approach is to use the inspect.isfunction()
method.
The inspect.isfunction()
method returns True
if the supplied object is a
Python function, otherwise False
is returned.
import inspect def do_math(a, b): return a + b print(inspect.isfunction(do_math)) # ๐๏ธ True if inspect.isfunction(do_math): # ๐๏ธ this runs print('The variable is a function') else: print('The variable is NOT a function')
The
inspect.isfunction()
method takes an object and returns True
if the object is a Python function.
The method would return False
if passed another callable, e.g. a class.
import inspect class Employee: pass print(inspect.isfunction(Employee)) # ๐๏ธ False print(inspect.isfunction(int)) # ๐๏ธ False
True
if provided a Python function.The method returns False
for most built-in functions because they are
implemented in C, not in Python.
import inspect print(inspect.isfunction(zip)) # ๐๏ธ False print(inspect.isfunction(map)) # ๐๏ธ False print(callable(zip)) # ๐๏ธ True print(callable(map)) # ๐๏ธ True
The zip
and map
functions are implemented in C, so the
inspect.isfunction()
method returned False
.
callable
function works as expected because it simply checks if the provided object is callable.types.FunctionType
You might also use the types.FunctionType
class to check if a variable is a
function.
import types def do_math(a, b): return a + b print(isinstance(do_math, types.FunctionType)) # ๐๏ธ True print(isinstance(zip, types.FunctionType)) # ๐๏ธ False print(isinstance(map, types.FunctionType)) # ๐๏ธ False
However, the class also returns True
only for Python functions.
The isinstance() function
returns True
if the passed-in object is an instance or a subclass of the
passed-in class.
The types.FunctionType
class works in the same way the inspect.isfunction()
method does.
However, the class is a bit more indirect, so there's no good reason to use it.
hasattr()
You can also use the hasattr()
function to check if a variable is a function.
def do_math(a, b): return a + b if hasattr(do_math, '__call__'): # ๐๏ธ this runs print('The variable is a function') else: print('The variable is NOT a function') print(hasattr(do_math, '__call__')) # ๐๏ธ True print(hasattr('bobbyhadz.com', '__call__')) # ๐๏ธ False
The hasattr() function takes the following 2 parameters:
Name | Description |
---|---|
object | The object we want to test for the existence of the attribute |
name | The name of the attribute to check for in the object |
The hasattr()
function returns True
if the string is the name of one of the
object's attributes, otherwise False
is returned.
If the object has a __call__
attribute then it is callable.
You can learn more about the related topics by checking out the following tutorials: