我有一个变量x,我想知道它是否指向一个函数。
我希望我能做一些像这样的事情:
>>> isinstance(x, function)
但这给了我:
Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name 'function' is not defined
我选这个是因为
>>> type(x)
<type 'function'>
我有一个变量x,我想知道它是否指向一个函数。
我希望我能做一些像这样的事情:
>>> isinstance(x, function)
但这给了我:
Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name 'function' is not defined
我选这个是因为
>>> type(x)
<type 'function'>
当前回答
在内置命名空间中没有构造函数的内置类型(例如函数、生成器、方法)在types模块中。你可以使用类型。isinstance调用中的函数类型:
>>> import types
>>> types.FunctionType
<class 'function'>
>>> def f(): pass
>>> isinstance(f, types.FunctionType)
True
>>> isinstance(lambda x : None, types.FunctionType)
True
注意,这里使用了一个非常具体的“函数”概念,这通常不是您所需要的。例如,它拒绝zip(严格来说是一个类):
>>> type(zip), isinstance(zip, types.FunctionType)
(<class 'type'>, False)
Open(内置函数有不同类型):
>>> type(open), isinstance(open, types.FunctionType)
(<class 'builtin_function_or_method'>, False)
和随机的。Shuffle(技术上是一种隐藏随机的方法。随机实例):
>>> type(random.shuffle), isinstance(random.shuffle, types.FunctionType)
(<class 'method'>, False)
如果你在做一些特定类型的事情。FunctionType实例,如反编译字节码或检查闭包变量,使用类型。FunctionType,但如果你只是需要一个对象像函数一样可调用,请使用callable。
其他回答
使用isinstance()和type(),它们都是Python中的内置函数,你可以检查它是否是一个函数,这样你就不需要导入任何东西:
def test():
pass
print(isinstance(test, type(test)))
输出:
True
被接受的答案在当时被认为是正确的。因为它 结果是,没有callable()的替代品,这又回到了Python中 3.2:具体来说,callable()检查对象的tp_call字段 测试。在普通的Python中没有对等的。大多数建议的测试都是 大多数时候是正确的:
>>> class Spam(object):
... def __call__(self):
... return 'OK'
>>> can_o_spam = Spam()
>>> can_o_spam()
'OK'
>>> callable(can_o_spam)
True
>>> hasattr(can_o_spam, '__call__')
True
>>> import collections
>>> isinstance(can_o_spam, collections.Callable)
True
方法中删除__call__来解决这个问题 类。为了让事情更有趣,在实例中添加一个假__call__ !
>>> del Spam.__call__
>>> can_o_spam.__call__ = lambda *args: 'OK?'
注意这个真的是不可调用的:
>>> can_o_spam()
Traceback (most recent call last):
...
TypeError: 'Spam' object is not callable
Callable()返回正确的结果:
>>> callable(can_o_spam)
False
但是hasattr错了:
>>> hasattr(can_o_spam, '__call__')
True
Can_o_spam确实有这个属性;它只是在调用时不使用 实例。
更微妙的是,isinstance()也会出错:
>>> isinstance(can_o_spam, collections.Callable)
True
因为我们之前使用了这个检查,后来删除了方法abc。ABCMeta 缓存结果。可以说这是abc.ABCMeta中的一个bug。也就是说, 没有比这更准确的结果了 结果比使用callable()本身,因为typeobject->tp_call 槽方法不能以任何其他方式访问。
只需使用callable()
在内置命名空间中没有构造函数的内置类型(例如函数、生成器、方法)在types模块中。你可以使用类型。isinstance调用中的函数类型:
>>> import types
>>> types.FunctionType
<class 'function'>
>>> def f(): pass
>>> isinstance(f, types.FunctionType)
True
>>> isinstance(lambda x : None, types.FunctionType)
True
注意,这里使用了一个非常具体的“函数”概念,这通常不是您所需要的。例如,它拒绝zip(严格来说是一个类):
>>> type(zip), isinstance(zip, types.FunctionType)
(<class 'type'>, False)
Open(内置函数有不同类型):
>>> type(open), isinstance(open, types.FunctionType)
(<class 'builtin_function_or_method'>, False)
和随机的。Shuffle(技术上是一种隐藏随机的方法。随机实例):
>>> type(random.shuffle), isinstance(random.shuffle, types.FunctionType)
(<class 'method'>, False)
如果你在做一些特定类型的事情。FunctionType实例,如反编译字节码或检查闭包变量,使用类型。FunctionType,但如果你只是需要一个对象像函数一样可调用,请使用callable。
如果这是针对Python 2。对于Python 3.2+,可以使用callable()。它曾经被弃用,但现在不弃用了,所以您可以再次使用它。你可以在这里阅读讨论:http://bugs.python.org/issue10518。你可以这样做:
callable(obj)
如果这是针对Python 3。X但在3.2之前,检查对象是否有__call__属性。你可以这样做:
hasattr(obj, '__call__')
经常被提及的类型。函数类型或检查。Isfunction方法(两者做的是完全相同的事情)有一些注意事项。对于非python函数,它返回False。例如,大多数内置函数是用C而不是Python实现的,因此它们返回False:
>>> isinstance(open, types.FunctionType)
False
>>> callable(open)
True
所以类型。FunctionType可能会给您带来令人惊讶的结果。检查鸭子类型对象属性的正确方法是询问它们是否嘎嘎叫,而不是查看它们是否适合鸭子大小的容器。
从Python 2.1开始,你可以从inspect模块导入isfunction。
>>> from inspect import isfunction
>>> def f(): pass
>>> isfunction(f)
True
>>> isfunction(lambda x: x)
True