如何检测 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() 函数
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() 函数,它们允许我们确定两个变量是否为相同类型。
数据结构
网络
关系型数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP