Search code examples
pythonperformancepython-importfunctools

Benefits to importing functools over own pure-Python impl?


I found this piece of code and started wondering, why not simply replace the import with own implementation? Is there any (performance) benefit to using functools.partial? Is it implemented in pure Python or native code?

try:
    from functools import partial
except ImportError:
    # http://docs.python.org/library/functools.html#functools.partial
    def partial(func, *args, **keywords):
        def newfunc(*fargs, **fkeywords):
            newkeywords = keywords.copy()
            newkeywords.update(fkeywords)
            return func(*(args + fargs), **newkeywords)

        newfunc.func = func
        newfunc.args = args
        newfunc.keywords = keywords
        return newfunc

Solution

  • Most of functools, including partial, is written in C, here is the header from functools.py

    """functools.py - Tools for working with functions and callable objects
    """
    # Python module wrapper for _functools C module
    # to allow utilities written in Python to be added
    # to the functools module
    

    In this specific quote, it could be said that the import is unnecessary, as if you have to define the fall back function anyway, why waste two lines on the import, I would argue that it is still beneficial as:

    1) The function from the library is probably faster, and even if it is not, it may become faster in a future release (the built ins do get optimised from time to time), or it may be faster on a different platform, or even a different implementation (pypy vs cpython for example). The reverse could also be true, which is why I think point 2 below is much more important, unless you have a specific performance problem.

    2) Any developer reviewing your code can understand what your partial function does - by referring to the standard documenation. Even if functools cannot be imported, any reader is going to be able to see from the attempted import what the pure python implementation is going to do, and how it should behave.