0

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
maazza
  • 7,016
  • 15
  • 63
  • 96
ArekBulski
  • 4,520
  • 4
  • 39
  • 61
  • 1
    `partial` is defined in `_functools`, which is a natively compiled library. I assume the reason is so that it can do something more efficient than simply wrap one callable object inside another. – chepner Dec 12 '15 at 17:16
  • 1
    At the beginning of `functools.py`, you should see something like `from _functools import partial`. That's what shows that `partial` is not implemented in pure Python. – chepner Dec 12 '15 at 18:00

1 Answers1

1

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.

James
  • 3,252
  • 1
  • 18
  • 33