Skip to content Skip to sidebar Skip to footer

Determine Whether A Python Function Is Already Implemented In C Extension

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 sl

Solution 1:

Check whether it is an instance of types.FunctionType:

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

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

C functions are instances of builtin_function_or_method:

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

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

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

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:

defis_implemented_in_c(obj):
    ifisinstance(obj, (types.FunctionType, types.LambdaType)):
        returnFalseelifisinstance(obj, type):
        if'__dict__'indir(obj): returnFalsereturnnothasattr(obj, '__slots__')
    # We accept also instances of classes.# Return True for instances of C classes, False for python classes.returnnotisinstance(obj, types.InstanceType)

Example usage:

>>>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
>>>classA(object):...    __slots__ = ('a', 'b')...>>>is_implemented_in_c(A)
False

Post a Comment for "Determine Whether A Python Function Is Already Implemented In C Extension"