首页 > 文章列表 > 如何检测一个Python变量是否为函数?

如何检测一个Python变量是否为函数?

Python 函数 变量
122 2023-09-06

在本文中,我们将学习如何检测一个Python变量是否为函数。

有时候,确定一个Python变量是否是一个函数是很重要的。当代码有上千行并且你不是代码的创建者时,这可能看起来毫无价值,你可能会质疑一个变量是否是一个函数。

Methods Used

以下是检查Python变量是否为函数的方法:

  • 使用内置的callable()函数

  • 使用 inspect 模块的 isfunction() 方法

  • 使用type()函数

  • 使用内置的 hasattr() 函数

  • 使用isinstance()函数

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)

算法(步骤)

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

  • 创建任意随机函数。这里的函数返回传递给它的两个数字的相加结果。

  • 使用 return 关键字返回传入的两个数字的和。

  • 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.

  • 创建一个变量来存储输入的数字。

  • 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))

输出

在执行时,上述程序将生成以下输出 -

True
False

方法2:使用inspect模块的isfunction()函数

inspect模块的isfunction()函数可以用于确定变量是否为函数。如果是函数,则返回布尔值True,否则返回False。

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

Example

以下程序使用inspect模块的isfunction()函数来检查一个Python变量是否是一个函数。

# 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))

输出

在执行时,上述程序将生成以下输出 -

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

以下程序使用type()函数来检查一个python变量是否是一个函数或者不是一个函数。

# 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))

输出

在执行时,上述程序将生成以下输出 -

<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__'))

输出

在执行时,上述程序将生成以下输出 -

True
False

方法5:使用isinstance()函数

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))

输出

在执行时,上述程序将生成以下输出 -

True
False

Conclusion

这篇文章教会了我们五种不同的方法来确定输入变量是否为函数类型。我们还熟悉了hasattr()和isinstance()函数,它们可以帮助我们确定两个变量是否属于相同的类型。