[Python-ideas] Preserving **kwargs order

Antony Lee antony.lee at berkeley.edu
Thu Mar 20 07:03:14 CET 2014


Somehow it feels wrong to add a fairly complex new feature to the language
(namely, ordered keyword arguments) to handle the very specific case of
ordered dict constructors (or is there some other (clearly) different use
intended?).  If anything, I prefer the abuse of "slicing" syntax (so that
one can write "od['a': 'b', 'c': 'd']" which is not much worse than "{'a':
'b', 'c': 'd'}").

Let's say we start with

def wrapper(**kwargs):
    <some code that manipulates kwargs>
    return g(**kwargs)

def g(**kwargs): pass

wrapper(a=1, b=2)

Nothing requires ordered keyword arguments, wrapper doesn't bother saving
the argument order, everything is fine (but because there is some code that
manipulates kwargs, an actual dict must be created, not just some sort of
proxy)... now, just after wrapper is executed and before g gets called,
globals()["g"] gets changed (by another thread...) to a function that does
require ordered keyword arguments.  What now?

Antony


2014-03-19 19:42 GMT-07:00 MRAB <python at mrabarnett.plus.com>:

> On 2014-03-20 01:58, Andrew Barnert wrote:> On Mar 19, 2014, at 16:32,
> Eric Snow <ericsnowcurrently at gmail.com> wrote:
> >
> >> Hopefully I'll have time to write a proto-PEP on this in the next
> >> couple weeks, but the gist is that I see 2 options:
> >
> > Last time around, someone suggested that ***kwargs could get you the
> > args as a list of pairs, or maybe an OrderedDict. While that would
> > require a bit of complexity in the CALL_FUNCTION code, it seems like
> > it would be simpler than your option 2.
> >
> > But it doesn't solve the main problem. Right now, you can forward
> > any arguments perfectly by doing this:
> >
> >      def wrapper(*args, **kwargs):
> >          return wrappee(*args, **kwargs)
> >
> > Your option 2 would require much more verbose code to forward
> > perfectly. The triple-star idea makes it a lot nicer (assuming that
> > ** passing respects the order, or a new *** is added there as well),
> > but it would still break the thousands of wrapper functions out
> > there written with **kwargs.
> >
>
> Wouldn't it be a problem only if the dict were unpacked and then
> repacked? In the code above, it's merely passing the input kwargs on to
> 'wrappee'.
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140319/1b016632/attachment.html>


More information about the Python-ideas mailing list