[Python-Dev] Partial function application 'from the right'

Xavier Morel catch-all at masklinn.net
Fri Feb 6 18:24:38 CET 2009


On 5 Feb 2009, at 23:54 , Steven D'Aprano wrote:
> Raymond Hettinger wrote:
>
>>>> The arguments for and against the patch could be brought against  
>>>> partial()
>>>> itself, so I don't understand the -1's at all.
>>>
>>> Quite so, but that doesn't justify adding more capabilities to  
>>> partial().
>> I concur with Collin.  Lever arguments are a road to bloat.
>
> One person's "bloat" is another person's rich and complete toolbox.
>
> > "In for a penny, in for a pound" is not a language design principle.
>
> Neither are "slippery slope" arguments.
>
>> One of the real problems with partial() and its variants is that  
>> they provide almost no advantage over an equivalent lambda.
>
> How about speed? I don't have a recent version of Python here to  
> test, but my recollection is that partial is significantly faster  
> than lambda. And even if it currently isn't, there could be (is?)  
> more opportunity to optimize partial.
>
> I guess that the voting on this is going to be fall along functional  
> lines: those who like functional languages will vote +1 on partial  
> and co, and those who don't will vote -1.
>
> While I don't dislike partial(), I'd rather see one good use-case  
> for partial_right() to be removed: built-ins that don't accept  
> keywords. From Ben North's post starting this thread:
>
> "I find 'functools.partial' useful, but occasionally I'm unable to  
> use it because it lacks a 'from the right' version.  E.g., to create  
> a function which splits a string on commas, you can't say
>
>   # Won't work when called:
>   split_comma = partial(str.split, sep = ',')
>
> and to create a 'log to base 10' function, you can't say
>
>   # Won't work when called:
>   log_10 = partial(math.log, base = 10.0)
>
> because str.split and math.log don't take keyword arguments."
>
Wouldn't a `flip` function (reverse the order of the function's  
arguments) be more flexible and general than a `partial_right` one?

Your first case would become

   # we're not ignoring any argument, so we have to provide `maxsplit`
   split_comma = partial(flip(str.split), None, ',')

and the second one would yield

   log_10 = partial(flip(math.log), 10.0)

and if we only want to fill-in the rightmost argument, we can flip the  
result to get the original order back:

   split_5 = flip(partial(flip(str.split), 5))

While better kwargs support would be even better, there probably  
always will be kwarg-less functions/methods, so the ability to  
partially apply from the right stays interesting.


More information about the Python-Dev mailing list