【问题标题】:How can I check whether a function is a function type in python? [duplicate]如何检查函数是否是python中的函数类型? [复制]
【发布时间】:2021-01-20 06:13:51
【问题描述】:

python中如何判断一个函数是否是函数类型?

我这样做了:

print(type(func_1))

python 告诉我这是一个 <class 'function'>

但是,当我这样做时:

print(type(func_1) is function)

我收到了一个名称错误 NameError: name 'function' is not defined

【问题讨论】:

  • 理想情况下,你根本不会做这个检查——Python中除了函数之外还有其他类型的可调用对象,你为什么要任意阻止它们的使用?

标签: python types


【解决方案1】:

您可以将其与 lambda 函数的类型进行比较(相同)

print(type(func_1) == type(lambda:None))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-24
    • 2011-08-25
    • 1970-01-01
    • 2020-09-21
    相关资源
    最近更新 更多