How to check if a variable is a Function in Python

avatar
Borislav Hadzhiev

Last updated: Apr 10, 2024
3 min

banner

# Table of Contents

  1. Check if a variable is a Function in Python
  2. Check if a variable is a Function using inspect.isfunction()
  3. Check if a variable is a Function using types.FunctionType
  4. Check if a variable is a Function using hasattr()

# Check if a variable is a Function in Python

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.

main.py
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')

check if variable is function

The code for this article is available on GitHub

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.

main.py
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.

Note that the 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.

main.py
class Employee: pass print(callable(Employee)) # ๐Ÿ‘‰๏ธ True print(callable(int)) # ๐Ÿ‘‰๏ธ True

# Check if a variable is a Function using inspect.isfunction()

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.

main.py
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')

check if variable is function using inspect isfunction

The code for this article is available on GitHub

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.

main.py
import inspect class Employee: pass print(inspect.isfunction(Employee)) # ๐Ÿ‘‰๏ธ False print(inspect.isfunction(int)) # ๐Ÿ‘‰๏ธ False
However, an important thing to note is that the method only returns True if provided a Python function.

The method returns False for most built-in functions because they are implemented in C, not in Python.

main.py
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.

The callable function works as expected because it simply checks if the provided object is callable.

# Check if a variable is a Function using types.FunctionType

You might also use the types.FunctionType class to check if a variable is a function.

main.py
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
The code for this article is available on GitHub

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.

# Check if a variable is a Function using hasattr()

You can also use the hasattr() function to check if a variable is a function.

main.py
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 code for this article is available on GitHub

The hasattr() function takes the following 2 parameters:

NameDescription
objectThe object we want to test for the existence of the attribute
nameThe 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.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev