Python 中检查变量是否函数
使用 callable()
函数检查变量是否为函数,例如 if callable(function):
。 callable()
函数接受一个对象,如果该对象是可调用的,则返回 True,否则返回 False。
def do_math(a, b):
return a + b
print(callable(do_math)) # 👉️ True
print(callable('hello')) # 👉️ False
if callable(do_math):
# 👇️ this runs
print('The variable is a function')
else:
print('The variable is NOT a function')
我们使用 callable()
内置函数来检查变量是否是函数。
callable
函数将对象作为参数,如果对象看起来是可调用的,则返回 True,否则返回 False。
print(callable(lambda x: x * 2)) # 👉️ True
print(callable(3.14)) # 👉️ False
如果 callable()
函数返回 True,调用对象仍有可能失败,但如果返回 False,则调用对象永远不会成功。
请注意
,callable()
函数不会检查传入的值是否专门为函数,它会检查该值是否可调用。
它还为类返回 True。
class Employee:
pass
print(callable(Employee)) # 👉️ True
print(callable(int)) # 👉️ True
另一种方法是使用 inspect.isfunction()
方法。
使用 inspect.isfunction() 检查变量是否为函数
使用 inspect.isfunction()
方法检查变量是否是函数,例如 if inspect.isfunction(function):
。 如果提供的对象是 Python 函数,则 inspect.isfunction()
方法返回 True,否则返回 False。
import inspect
def get_len(a):
return len(a)
print(inspect.isfunction(get_len)) # 👉️ True
if inspect.isfunction(get_len):
# 👇️ this runs
print('The variable is a function')
print('The Site:www.jiyik.com lenth:'+ str(get_len('www.jiyik.com')))
else:
print('The variable is NOT a function')
inspect.isfunction
方法接受一个对象,如果该对象是 Python 函数,则返回 True。
如果传递另一个可调用对象,该方法将返回 False,例如 一个类。
import inspect
class Employee:
pass
print(inspect.isfunction(Employee)) # 👉️ False
print(inspect.isfunction(int)) # 👉️ False
但是,需要注意的重要一点是,该方法仅在提供 Python 函数时才返回 True。
对于大多数内置函数,该方法返回 False,因为它们是在 C 中实现的,而不是在 Python 中。
import inspect
print(inspect.isfunction(zip)) # 👉️ False
print(inspect.isfunction(map)) # 👉️ False
print(callable(zip)) # 👉️ True
print(callable(map)) # 👉️ True
zip
和 map
函数是用 C 实现的,因此 inspect.isfunction()
方法返回 False。
可调用函数按预期工作,因为它只是检查提供的对象是否可调用。
我们还可以使用 types.FunctionType
类来检查变量是否为函数。
import types
def do_math(a, b):
return a + b
print(isinstance(do_math, types.FunctionType)) # 👉️ True
print(isinstance(zip, types.FunctionType)) # 👉️ False
print(isinstance(map, types.FunctionType)) # 👉️ False
但是,该类也仅对 Python 函数返回 True。
如果传入的对象是传入类的实例或子类,则 isinstance
函数返回 True。
types.FunctionType
类的工作方式与 inspect.isfunction()
方法的工作方式相同。
然而,这个类有点间接,所以没有充分的理由使用它。
相关文章
Python pandas.pivot_table() 函数
发布时间:2024/04/24 浏览次数:82 分类:Python
-
Python Pandas pivot_table()函数通过对数据进行汇总,避免了数据的重复。
在 Python 中将 Pandas 系列的日期时间转换为字符串
发布时间:2024/04/24 浏览次数:894 分类:Python
-
了解如何在 Python 中将 Pandas 系列日期时间转换为字符串
在 Python Pandas 中使用 str.split 将字符串拆分为两个列表列
发布时间:2024/04/24 浏览次数:1124 分类:Python
-
本教程介绍如何使用 pandas str.split() 函数将字符串拆分为两个列表列。
在 Pandas 中将 Timedelta 转换为 Int
发布时间:2024/04/23 浏览次数:231 分类:Python
-
可以使用 Pandas 中的 dt 属性将 timedelta 转换为整数。
Python 中的 Pandas 插入方法
发布时间:2024/04/23 浏览次数:112 分类:Python
-
本教程介绍了如何在 Pandas DataFrame 中使用 insert 方法在 DataFrame 中插入一列。
使用 Python 将 Pandas DataFrame 保存为 HTML
发布时间:2024/04/21 浏览次数:106 分类:Python
-
本教程演示如何将 Pandas DataFrame 转换为 Python 中的 HTML 表格。
如何将 Python 字典转换为 Pandas DataFrame
发布时间:2024/04/20 浏览次数:73 分类:Python
-
本教程演示如何将 python 字典转换为 Pandas DataFrame,例如使用 Pandas DataFrame 构造函数或 from_dict 方法。
如何在 Pandas 中将 DataFrame 列转换为日期时间
发布时间:2024/04/20 浏览次数:101 分类:Python
-
本文介绍如何将 Pandas DataFrame 列转换为 Python 日期时间。