确定Python函数是否已在C扩展中实现 [英] Determine whether a Python function is already implemented in C extension

查看:56
本文介绍了确定Python函数是否已在C扩展中实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个Python程序,在配置文件后运行缓慢,并且已经确定了瓶颈.我导入的第三方模块中的一个特定功能特别慢.

Suppose I have a Python program that runs slow- after profiliing and I have identified the bottleneck. One particular function from a 3rd party module I imported is particularly slow.

对于这种特殊情况,我知道函数是用Python实现的(使用Eclipse,很容易跳转到函数定义).因此,我知道我可以将该功能转换为Cython作为加速选项.(如果已经用C实现了,那么用Cython编写它就没有意义了.).

For this particular case, I know that function is implemented in Python (Used Eclipse and it's easy to jump to the function definition). So I know that I can convert that function into Cython as a speed-up option. (If it is already implemented in C, there is no point in writing it in Cython...).

如果我没有IDE,那么确定这一点的简便方法是什么?我知道我可以转到模块安装目录,如果模块位于.so中,则可以推断它位于C中.但是还有其他选择吗?

If I don't have an IDE, what would be an easy option to determine this? I know that I can go to the directory where the module is installed and infer that it is in C if the module is in .so. But is there any alternative?

谢谢

推荐答案

检查它是否是

Check whether it is an instance of types.FunctionType:

>>> import types
>>> isinstance(len, types.FunctionType)
False
>>> def mylen(): pass
... 
>>> isinstance(mylen, types.FunctionType)
True

也许您会更安全地检查 isinstance(X,(types.FunctionType,types.LambdaType).

Probably you'd be safer to check for isinstance(X, (types.FunctionType, types.LambdaType).

C函数是 builtin_function_or_method 的实例:

>>> len.__class__
<type 'builtin_function_or_method'>
>>> np.vdot.__class__
<type 'builtin_function_or_method'>

您可以使用 types.BuiltinFunctionType / types.BuiltinMethodType 来访问此类型.

You can access this type as types.BuiltinFunctionType/types.BuiltinMethodType.

或者,您可以检查该函数是否具有 __ code __ 属性.由于C函数没有字节码,因此它们不能具有 __ code __ .

Alternatively you can check whether the function has a __code__ attribute. Since C functions do not have bytecode, they can't have __code__.

请注意,有时看起来像函数的实际上是 class ,例如枚举,但是某些第三方库可能会执行相同的操作.这意味着您还应该检查是否在C中实现了一个类.因为所有类都是 type 的实例,所以这很难.一种方法可能是检查类的 dir 中是否有 __ dict __ ,如果没有,则应检查 __ slots __ .

Note sometimes what seems like a function is actually a class, e.g. enumerate but some 3rd party library may do the same. This means that you should also check whether a class is implemented in C or not. This one is harder since all classes are instances of type. A way may be to check whether the class has a __dict__ in its dir, and if it doesn't have you should check for __slots__.

类似以下内容应该很准确:

Something like the following should be pretty accurate:

def is_implemented_in_c(obj):
    if isinstance(obj, (types.FunctionType, types.LambdaType)):
        return False
    elif isinstance(obj, type):
        if '__dict__' in dir(obj): return False
        return not hasattr(obj, '__slots__')
    # We accept also instances of classes.
    # Return True for instances of C classes, False for python classes.
    return not isinstance(obj, types.InstanceType)

示例用法:

>>> is_implemented_in_c(enumerate)
True
>>> is_implemented_in_c(len)
True
>>> is_implemented_in_c(np.vdot)
True
>>> is_implemented_in_c(lambda x: True)
False
>>> is_implemented_in_c(object)
True
>>> class A(object):
...     __slots__ = ('a', 'b')
... 
>>> is_implemented_in_c(A)
False

这篇关于确定Python函数是否已在C扩展中实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆