3

From the functools documentation, the partial function is "roughly equivalent to"

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

However, I don't understand the need for the lines

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

in the definition. If I create a partial function without these three lines it behaves exactly the same as the definition including those lines. The documentation page says that the partial object must have the three attributes func, args, and keywords, so I can see why the above function definition inludes these, but I still don't see what tangible goal they accomplish.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
deepak
  • 2,045
  • 2
  • 21
  • 36
  • 3
    They're are not needed for the `partial` to work, I think, it's just in case if you need some introspection later. – bereal Feb 08 '15 at 15:09
  • 5
    Per [this answer](http://stackoverflow.com/a/3252425/3001761), *"[the] returned function is decorated with attributes useful for introspection -- the function it's wrapping, and what positional and named arguments it fixes therein"* – jonrsharpe Feb 08 '15 at 15:12
  • Ok cool! That the lines are there for introspection purpose was my operating assumption but I wasn't completely confident about that. – deepak Feb 08 '15 at 15:18

0 Answers0