[Python-Dev] repeated keyword arguments
Duncan Booth
duncan.booth at suttoncourtenay.org.uk
Wed Jul 2 13:47:02 CEST 2008
"Steven D'Aprano" <steve at pearwood.info> wrote:
> It would be nice to be able to do this:
>
> defaults = dict(a=5, b=7)
> f(**defaults, a=8) # override the value of a in defaults
>
> but unfortunately that gives a syntax error. Reversing the order would
> override the wrong value. So as Python exists now, no, it's not
> terribly useful. But it's not inherently a stupid idea.
There is already an easy way to do that using functools.partial, and it is
documented and therefore presumably deliberate behaviour "If additional
keyword arguments are supplied, they extend and override keywords."
>>> from functools import partial
>>> def f(a=1, b=2, c=3):
print a, b, c
>>> g = partial(f, b=99)
>>> g()
1 99 3
>>> g(a=100, b=101)
100 101 3
More information about the Python-Dev
mailing list