How to detect whether a Python variable is a function?


In this article, we will learn how to detect whether a Python variable is a function.

Sometimes, it is essential to figure out whether or not a Python variable is a function. When the code is a thousand lines long and you are not the creator of the code, this may appear to be of little value, and you may find yourself questioning if a variable is a function or not.

Methods Used

The following are the methods to check whether a python variable is a function −

  • Using the built-in callable() function

  • Using the isfunction() of the inspect module

  • Using the type() function

  • Using the built-in hasattr() function

  • Using the isinstance() function

Method 1: Using the built-in callable() function

The callable() function returns a boolean result. It returns True if the function is callable else it returns False.

Syntax

callable(object)

Algorithm (Steps)

Following are the Algorithms/steps to be followed to perform the desired task. −

  • Create any random function. The function here returns the addition of two numbers passed to it.

  • Use the return keyword to return the sum of the passed two numbers.

  • Use the callable() function to check whether the object passed ( i.e, addition) is a function or NOT. It returns True if it is a function else False.

  • Create a variable to store an input number.

  • Similarly, check whether the variable 'number' is a function or not using the callable() function.

Example

The following program checks whether a python variable is a function or not using the built-in callable() function −

# creating a function that returns the addition 
# of 2 numbers passed to it
def addition(p, q):
   # returning the sum of the given two numbers(arguments) 
      return p+q

# using the callable() function to check whether 
# the variable 'addition' is a function 
# it returns True if it is a function else False
print(callable(addition))
number = 10
# checking whether the variable 'number' is a function
print(callable(number))

Output

On execution, the above program will generate the following output −

True
False

Method 2: Using the isfunction() of the inspect module

The isfunction() function of the inspect module can be used to determine if the variable is a function or not. It returns a boolean value True if it is a function else returns False.

Additionally, to utilize this, you must first import isfunction from inspect module before using it to obtain a boolean value.

Example

The following program checks whether a python variable is a function or not using the isfunction() function of the inspect module −

# importing isfunction from inspect module
from inspect import isfunction

# creating a function that returns the addition 
# of 2 numbers passed to it
def addition(p, q):
   # returning the sum of the given two numbers(arguments)
      return p+q
 
# using the isfunction() function to check whether 
# the variable 'addition' is a function or not 
# it returns True if it is a function else False
print(isfunction(addition)) 
number = 10

print(isfunction(number))

Output

On execution, the above program will generate the following output −

True
False

Method 3: Using the type() function

The type() function identifies the type of an object so that we may determine whether it is callable or not based on whether the object is of the function type.

In simple terms, the type() function returns the data type of an object.

Example

The following program checks whether a python variable is a function or not using the type() function −

# creating a function that returns the addition 
# of 2 numbers passed to it
def addition(p, q):
   # returning the sum of the given two numbers(arguments) 
      return p+q
 
# checking the type of the variable by passing 
# the variable as an argument to it
print(type(addition))
# given Variable
number = 10

print(type(number))

Output

On execution, the above program will generate the following output −

<class 'function'>
<class 'int'>

Method 4: Using the built-in hasattr() function

The hasattr() is a function that identifies the type of an object so that we can determine whether or not that object type is a function. In the same way as callable(), it also returns a boolean value.

Example

The following program checks whether a python variable is a function or not using the built-in hasattr() function −

# creating a function that returns the addition 
# of 2 numbers passed to it
def addition(p, q):
   # returning the sum of the given two numbers(arguments)
   return p+q
 
# checking whether the type of variable 'addition' 
# is a function or not using hasattr() function
# it returns True if it is a function else False
print(hasattr(addition, '__call__'))
number = 10
# checking whether the variable 'number' is a function or not
print(hasattr(number, '__call__'))

Output

On execution, the above program will generate the following output −

True
False

Method 5: Using the isinstance() function

The isinstance() is a function that identifies the type of an object so that we may determine whether or not that object is a function. It returns a boolean value.

Example

The following program checks whether a python variable is a function or not using isinstance() function −

# importing the types module
import types

# creating a function that returns the addition 
# of 2 numbers passed to it
def addition(p, q):
   # # returning the sum of the given two numbers(arguments) 
   return p+q

# passing object,  types.FunctionType as an argument to the 
# isinstance() function to check whether it is a function or not
print(isinstance(addition, types.FunctionType))
number = 10
# checking whether the above variable 'number' is a function or not
print(isinstance(number, types.FunctionType))

Output

On execution, the above program will generate the following output −

True
False

Conclusion

This article taught us five distinct ways to determine if an input variable is a function type. We also became familiar with the hasattr() and isinstance() functions, which allow us to determine whether or not two variables are of the same type.

Updated on: 24-Jan-2023

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements