如果对象是任何函数类型,是否有一种常见的方法来检查Python?

时间:2021-10-06 01:55:04

I have a function in Python which is iterating over the attributes returned from dir(obj), and I want to check to see if any of the objects contained within is a function, method, built-in function, etc. Normally you could use callable() for this, but I don't want to include classes. The best I've come up with so far is:

我在Python中有一个函数迭代从dir(obj)返回的属性,我想检查其中包含的任何对象是否是函数,方法,内置函数等。通常你可以使用callable()为此,但我不想包含类。到目前为止,我想出的最好的是:

isinstance(obj, (types.BuiltinFunctionType, types.FunctionType, types.MethodType))

Is there a more future-proof way to do this check?

是否有更加面向未来的方法来进行此项检查?

Edit: I misspoke before when I said: "Normally you could use callable() for this, but I don't want to disqualify classes." I actually do want to disqualify classes. I want to match only functions, not classes.

编辑:我错过了之前我说:“通常你可以使用callable(),但我不想取消课程资格。”我其实想要取消课程资格。我想只匹配函数,而不是类。

4 个解决方案

#1


13  

The inspect module has exactly what you want:

检查模块正是您想要的:

inspect.isroutine( obj )

FYI, the code is:

仅供参考,代码是:

def isroutine(object):
    """Return true if the object is any kind of function or method."""
    return (isbuiltin(object)
            or isfunction(object)
            or ismethod(object)
            or ismethoddescriptor(object))

#2


5  

If you want to exclude classes and other random objects that may have a __call__ method, and only check for functions and methods, these three functions in the inspect module

如果要排除可能具有__call__方法的类和其他随机对象,并且仅检查函数和方法,则检查模块中的这三个函数

inspect.isfunction(obj)
inspect.isbuiltin(obj)
inspect.ismethod(obj)

should do what you want in a future-proof way.

应该以面向未来的方式做你想做的事。

#3


2  

if hasattr(obj, '__call__'): pass

This also fits in better with Python's "duck typing" philosophy, because you don't really care what it is, so long as you can call it.

这也适用于Python的“鸭子打字”理念,因为你并不关心它是什么,只要你可以调用它。

It's worth noting that callable() is being removed from Python and is not present in 3.0.

值得注意的是,callable()正在从Python中删除,并且不存在于3.0中。

#4


1  

Depending on what you mean by 'class':

取决于你所说的“课堂”:

callable( obj ) and not inspect.isclass( obj )

or:

callable( obj ) and not isinstance( obj, types.ClassType )

For example, results are different for 'dict':

例如,'dict'的结果不同:

>>> callable( dict ) and not inspect.isclass( dict )
False
>>> callable( dict ) and not isinstance( dict, types.ClassType )
True

#1


13  

The inspect module has exactly what you want:

检查模块正是您想要的:

inspect.isroutine( obj )

FYI, the code is:

仅供参考,代码是:

def isroutine(object):
    """Return true if the object is any kind of function or method."""
    return (isbuiltin(object)
            or isfunction(object)
            or ismethod(object)
            or ismethoddescriptor(object))

#2


5  

If you want to exclude classes and other random objects that may have a __call__ method, and only check for functions and methods, these three functions in the inspect module

如果要排除可能具有__call__方法的类和其他随机对象,并且仅检查函数和方法,则检查模块中的这三个函数

inspect.isfunction(obj)
inspect.isbuiltin(obj)
inspect.ismethod(obj)

should do what you want in a future-proof way.

应该以面向未来的方式做你想做的事。

#3


2  

if hasattr(obj, '__call__'): pass

This also fits in better with Python's "duck typing" philosophy, because you don't really care what it is, so long as you can call it.

这也适用于Python的“鸭子打字”理念,因为你并不关心它是什么,只要你可以调用它。

It's worth noting that callable() is being removed from Python and is not present in 3.0.

值得注意的是,callable()正在从Python中删除,并且不存在于3.0中。

#4


1  

Depending on what you mean by 'class':

取决于你所说的“课堂”:

callable( obj ) and not inspect.isclass( obj )

or:

callable( obj ) and not isinstance( obj, types.ClassType )

For example, results are different for 'dict':

例如,'dict'的结果不同:

>>> callable( dict ) and not inspect.isclass( dict )
False
>>> callable( dict ) and not isinstance( dict, types.ClassType )
True