functools.partial [was Re: and on - topic and and off topic]

Steven D'Aprano steve at pearwood.info
Fri Jul 29 02:20:42 EDT 2016


On Fri, 29 Jul 2016 12:23 am, Sivan Greenberg wrote:

> 1. When is the use of functools.partial beneficial? When can it be a
> hindrance? Perhaps it can save on func argument evaluation time when
> creating many invocations for asycn exec?

I'm not sure that partial is intended as an optimization. It may end up
saving time by avoiding evaluating arguments, but that's not why it exists.
It exists to enable the functional programming idiom of partial evaluation
in a simpler, more idiomatic (for functional programmers) way:

function_of_one_argument = function_of_two_arguments(arg)

rather than:

def function_of_one_argument(x):
    return function_of_two_arguments(arg, x)


If it happens to be faster than the alternative, that's a bonus.

I don't know whether or not it saves time when invoking async exec. Have you
tried it to see?



-- 
Steven
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.




More information about the Python-list mailing list