functools.partial doesn't work without using named parameter

Benjamin Kaplan benjamin.kaplan at case.edu
Fri Mar 25 03:03:37 EDT 2011


On Fri, Mar 25, 2011 at 2:30 AM, Paddy <paddy3118 at googlemail.com> wrote:
> Hi, I just found the following oddity where for function fsf1 I am forced to use a named parameter for correct evaluation and was wondering why it doesn't work, yet the example from the docs of wrapping int to create basetwo doesn't need this?
> The example:
>
>>>> from functools import partial
>>>> basetwo = partial(int, base=2)
>>>> basetwo('10010')
> 18
>>>>
>>>> def fs(f, s): return [f(value) for value in s]
>
>>>> def f1(value): return value * 2
>
>>>> s = [0, 1, 2, 3]
>>>> fs(f1, s)
> [0, 2, 4, 6]
>>>>
>>>> fsf1 = partial(fs, f=f1)
>>>> fsf1(s)
> Traceback (most recent call last):
>  File "<pyshell#24>", line 1, in <module>
>    fsf1(s)
> TypeError: fs() got multiple values for keyword argument 'f'
>>>> # BUT
>>>> fsf1(s=s)
> [0, 2, 4, 6]
>>>>
>
> Would someone help?
>

If you hand partial a keyword argument, it treats it as a keyword
argument. If you want to hand it a positional argument, hand it a
positional argument.

>>> fsf1 = partial(fs,f1)
>>> fsf1(s)
[0, 2, 4, 6]



More information about the Python-list mailing list