我有一个变量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'>
当前回答
一个精确的功能检查器
Callable是一个很好的解决方案。然而,我想用与John Feminella相反的方式来对待这个问题。不要像这样对待它:
检查鸭子类型对象属性的正确方法是询问它们是否嘎嘎叫,而不是查看它们是否适合鸭子大小的容器。“直接比较”的方法会对许多函数给出错误的答案,比如内置函数。
我们会这样对待它:
判断一个东西是不是鸭子的正确方法不是看它会不会嘎嘎叫,而是通过几个过滤器来判断它是否真的是一只鸭子,而不是仅仅从表面上看它是否像一只鸭子。
我们将如何实现它
'types'模块有很多类来检测函数,最有用的是类型。FunctionType,但也有很多其他类型,比如方法类型、内置类型和lambda类型。我们还将考虑一个'functools '。作为函数的Partial对象。
检查它是否是函数的简单方法是对所有这些类型使用isinstance条件。以前,我想创建一个继承上述所有类的基类,但我无法做到这一点,因为Python不允许我们继承上面的一些类。
下面是一个表,说明什么类可以分类什么函数:
以上函数表由kinght-金编写
实现它的代码
现在,这段代码完成了我们上面描述的所有工作。
from types import BuiltinFunctionType, BuiltinMethodType, FunctionType, MethodType, LambdaType
from functools import partial
def is_function(obj):
return isinstance(obj, (BuiltinFunctionType, BuiltinMethodType, FunctionType, MethodType, LambdaType, partial))
#-------------------------------------------------
def my_func():
pass
def add_both(x, y):
return x + y
class a:
def b(self):
pass
check = [
is_function(lambda x: x + x),
is_function(my_func),
is_function(a.b),
is_function(partial),
is_function(partial(add_both, 2))
]
print(check)
>>> [True, True, True, False, True]
一个false是is_function(partial),因为那是一个类,而不是一个函数,而这正是函数,而不是类。下面是一个预览,您可以从中试用代码。
结论
Callable (obj)是检查对象是否是函数的首选方法,如果您想通过鸭子类型检查绝对值。
我们的自定义is_function(obj),如果你没有将任何可调用的类实例作为函数,而只是内置或lambda, def或partial定义的函数,那么可能经过一些编辑后,它是检查对象是否是函数的首选方法。
我想这就是全部内容了。祝你有愉快的一天!
其他回答
这里有一些其他的方法:
def isFunction1(f) :
return type(f) == type(lambda x: x);
def isFunction2(f) :
return 'function' in str(type(f));
我是这样想到第二个的:
>>> type(lambda x: x);
<type 'function'>
>>> str(type(lambda x: x));
"<type 'function'>"
# Look Maa, function! ... I ACTUALLY told my mom about this!
这是我的代码:
# -*- coding: utf-8 -*-
import hashlib
import inspect
# calc everything to md5!!
def count_md5(content):
if isinstance(content, dict):
return count_md5(
[(str(k), count_md5(content[k])) for k in sorted(content.keys())],
)
elif isinstance(content, (list, tuple)):
content = [count_md5(k) for k in content]
elif callable(content):
return make_callable_hash(content)
return calc_md5(str(content))
def calc_md5(content):
m2 = hashlib.md5()
if isinstance(content, str):
m2.update(content.encode("utf8"))
else:
m2.update(content)
return m2.hexdigest()
def make_callable_hash(content):
if inspect.isclass(content):
h = []
for attr in [i for i in sorted(dir(content)) if not i.startswith("__")]:
v = getattr(content, attr)
h.append(count_md5(v))
return calc_md5("".join(h))
return calc_md5(content.__name__)
对于callable,大多数时候我们只是想看看属性的值是否一致,所以我们可以取callable的所有属性并计算它。 如果它是一个类'callable'将返回true,所以它不是很严格
这对我来说很管用:
str(type(a))=="<class 'function'>"
被接受的答案在当时被认为是正确的。因为它 结果是,没有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()
使用isinstance()和type(),它们都是Python中的内置函数,你可以检查它是否是一个函数,这样你就不需要导入任何东西:
def test():
pass
print(isinstance(test, type(test)))
输出:
True