Python 如何检测变量是否是函数
在本文中,我们将学习如何检测Python变量是否是函数。
有时候,判断Python变量是否是函数非常重要。当代码有上千行,并且你不是代码的创建者时,这可能显得不那么重要,你可能会质疑一个变量是否是函数。
使用的方法
以下是检查Python变量是否为函数的方法:
- 使用内置的callable()函数
-
使用inspect模块的isfunction()
-
使用type()函数
-
使用内置的hasattr()函数
-
使用isinstance()函数
方法1:使用内置的callable()函数
callable()函数返回一个布尔值。如果函数是可调用的,则返回True,否则返回False。
语法
callable(object)
步骤
以下是执行所需任务的算法/步骤。
- 创建任意随机函数。这里的函数返回传递给它的两个数字的加法。
-
使用 return 关键字返回传递的两个数字的总和。
-
使用 callable() 函数检查传递的对象(即addition)是否为函数。如果是函数,则返回True,否则返回False。
-
创建一个变量来存储输入的数字。
-
同样地,使用callable()函数检查变量 ‘number’ 是否为函数。
示例
以下程序使用内置的callable()函数检查一个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
# 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。
此外,在使用该函数之前,您必须先从inspect模块中导入isfunction函数才能使用它获得一个布尔值。
示例
以下程序使用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
方法3:使用type()函数
type()函数可以识别对象的类型,从而确定对象是否可调用,根据对象是否为函数类型来判断。
简单来说,type()函数返回一个对象的数据类型。
示例
以下程序使用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'>
方法4:使用内置的hasattr()函数
hasattr()是一个函数,它可以识别对象的类型,以便我们可以确定该对象类型是否为函数。与callable()函数一样,它也返回一个布尔值。
示例
下面的程序使用内置的hasattr()函数来检查一个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 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() 函数用于确定对象的类型,以便我们可以确定该对象是否为函数。它返回一个布尔值。
示例
下面的程序使用isinstance()函数来检查一个Python变量是否为函数:
# 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
结论
本文教会了我们五种不同的方法来确定一个输入变量是否为函数类型。我们还熟悉了hasattr()和isinstance()函数,它们可以帮助我们确定两个变量是否为相同类型。