Search code examples
pythoncounterpython-importpython-c-api

Is the Python Module Counter() Using C?


When I use the function:

from collections import Counter

Is Counter() sourced using C structures? Is there any convenient way to determine that generally for this and other function?

Give that Python is open sourced generally what should I be looking for in the source code if it is the best way to determine if a given function uses C structures?


Solution

  • There's no direct way to assert this for classes, but there's a way to determine if a function or method was written in C. So any class written in C probably defines at least one explicit or implicit function in C (if not it will be difficult) and you can check with inspect.isbuiltin:

    >>> import collections
    >>> import inspect
    >>> def is_c_class(cls):
    ...     return any(inspect.isbuiltin(getattr(cls, methname, None)) 
    ...                for methname in cls.__dict__)
    
    >>> is_c_class(collections.Counter)
    False
    
    >>> is_c_class(dict)
    True
    

    However that's not all there is to it because collections.Counter calls collections._count_elements which is a C function:

    >>> inspect.isbuiltin(collections._count_elements)
    True
    

    So you should always check the source code (Pythons repository is on github and also the implementation for Counter) to be sure.


    Note that the above mentioned check using isbuiltin has some flaws. For example you could have a class attribute that is a C function:

    >>> class Test(object):
    ...     c_function = all  # builtin all function is a C function
    
    >>> is_c_class(Test)
    True
    

    So don't rely on it always giving correct results, treat it as approximation.