微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

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

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

Methods Used

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

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()函数,它们可以帮助我们确定两个变量是否属于相同的类型。

以上就是如何检测一个Python变量是否为函数?的详细内容,更多请关注编程之家其它相关文章

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐