Home  >  Article  >  Backend Development  >  How to check if a Python variable is a function?

How to check if a Python variable is a function?

WBOY
WBOYforward
2023-08-26 14:05:031312browse

How to check if 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 important to determine whether a Python variable is a function. This may seem worthless when there are thousands of lines of code and you are not the creator of the code, and you may question whether a variable is a function.

Methods Used

Here’s how to check if a Python variable is a function:

  • Use the built-in callable() function

  • Use the isfunction() method of the inspect module

  • Use type() function

  • Use the built-in hasattr() function

  • Use 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 result of adding the two numbers passed to it.

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

  • 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 the entered 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

When executed, the above program will generate 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, returns the Boolean value True, otherwise 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 uses the isfunction() function of the inspect module to check whether a Python variable is a function.

# 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

When executed, 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 uses the type() function to check whether a python variable is a function or not.

# 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

When executed, 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

When executed, the above program will generate the following output -

True
False

Method 5: Use 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

When executed, the above program will generate the following output -

True
False

Conclusion

This article teaches us five different methods to determine whether the input variable is a function type. We are also familiar with the hasattr() and isinstance() functions, which can help us determine whether two variables are of the same type.

The above is the detailed content of How to check if a Python variable is a function?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete