How to detect if a Python variable is a function?

How to Check if a Python Variable is a Function?

This article will explain how to check if a Python variable is a function.

Sometimes, it’s important to know for sure whether a Python variable is a function. When the code is thousands of lines long and you’re not the author of the code, this might seem pointless, and you might ask yourself if the variable is a function.

Methods Used

Here are the methods for checking whether a Python variable is a function:

  • Using the built-in callable() function

  • Using the isfunction() function 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, and False otherwise.

Syntax

callable(object)

Algorithm (Steps)

Below are the algorithm/steps to perform the required task.

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

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

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

  • Create a variable to store the input number.

  • Similarly, use the callable() function to check if the variable ‘number’ is a function.

Example

The following program uses the built-in callable() function to check if a Python variable is a function:

# Create a function that returns the addition of two numbers passed to it
def addition(p, q):
# Returns the sum of the two given numbers (arguments)
return p+q
# Use the callable() function to check if the variable 'addition' is a function
# Returns True if it is a function, otherwise returns False
print(callable(addition))

number = 10
# Use the callable() function to check if the variable 'number' is a function
print(callable(number))

Output

Executing the above program will produce the following output:

True
False

Method 2: Use the isfunction() function of the inspect module

The isfunction() function of the inspect module can be used to determine whether a variable is a function. If it is a function, it returns a Boolean value of True; otherwise, it returns False.

Also, you must first import the isfunction function from the inspect module to obtain a Boolean value.

Example

The following program uses the isfunction() function from the inspect module to check whether a Python variable is a function:

# Import isfunction from the inspect module
from inspect import isfunction
# Create a function that returns the addition of two numbers passed to it
def addition(p, q):
# Returns the sum of the two given numbers (arguments)
return p+q

# Use the isfunction() function to check whether the variable 'addition' is a function
# Returns True if it is a function, otherwise returns False
print(isfunction(addition))

number = 10
# Use the isfunction() function to check whether the variable 'number' is a function
print(isfunction(number))

Output

Executing the above program will produce the following output:

True
False

Method 3: Using the type() Function

The type() function identifies the type of an object, so we can determine whether it is callable based on whether it is a function.

Simply put, the type() function returns the data type of an object.

Example

The following program uses the type() function to check whether a Python variable is a function.

# Create a function that returns the sum of two numbers
def addition(p, q):
# Returns the sum of two given numbers (arguments)

return p+q

# Check the type of a variable by passing it as an argument
print(type(addition))
# Given variable
number = 10

print(type(number))

Output

Running the above program will produce the following output –

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

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

hasattr() is a function that identifies the type of an object so we can determine if it is a function. Like callable(), it also returns a Boolean value.

Example

The following program uses the built-in hasattr() function to check whether a Python variable is a function.

# Create a function that returns the sum of two numbers
def addition(p, q):
# Returns the sum of the two given numbers (arguments)
return p+q

# Use the hasattr() function to check if the variable addition is a function
# Returns True if it is a function; otherwise, returns False
print(hasattr(addition, '__call__'))
number = 10
# Check if the variable number is a function
print(hasattr(number, '__call__'))

Output

Running the above program will produce the following output –

True
False

Method 5: Using the isinstance() Function

isinstance() is a function that identifies the type of an object so we can determine if it is a function. It returns a Boolean value.

Example

The following program uses the isinstance() function to check whether a Python variable is a function.

# Import the types module
import types

# Create a function that returns the sum of two numbers
def addition(p, q):
# # Returns the sum of two given numbers (arguments)
return p+q

# Pass the types.FunctionType object as a parameter to the isinstance() function
# Check if it is a function
print(isinstance(addition, types.FunctionType))
number = 10
# Check if the variable number is a function
print(isinstance(number, types.FunctionType))
<h3>Output</h3>
<p>Running the above program will produce the following output –</p>
<pre><code class="language-shell">True
False

Conclusion

This article introduced five different methods for determining whether an input variable is a function type. We also became familiar with the hasattr() and isinstance() functions, which allow us to determine whether two variables are of the same type.

Leave a Reply

Your email address will not be published. Required fields are marked *