[Python-ideas] Preserving **kwargs order (was: Re: OrderedDict literals)

Eric Snow ericsnowcurrently at gmail.com
Thu Mar 20 00:32:27 CET 2014

On Wed, Mar 19, 2014 at 4:01 PM,  <random832 at fastmail.us> wrote:
> On Wed, Mar 19, 2014, at 17:24, Eric Snow wrote:
>> FYI I'm working on a proposal to support preserving the call order of
>> **kwargs.  This would allow initializing an OrderedDict correctly
>> using keyword args.  I may have something to show for it by the end of
>> the PyCon sprints.
> I know that it's possible to support this* in dict, but is kwargs
> required to be a dict?

I'm not talking about the ** unpacking syntax, but about the function
definition syntax for **kwargs:

  def spam(a, b, **kwargs): ...

> What route are you going: modify dict to allow
> this and require dict to support it; make a new class for kwargs and
> require this behavior of kwargs; modify dict on CPython to allow it but
> only require that kwargs support it "somehow" on non-CPython?

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:

1. Change kwargs to an OrderedDict for functions decorated a special decorator:

  def spam(a, b, **kwargs):
     ...  # kwargs will be an OrderedDict

2. Store the ordered keys in a list and bind that to a special local
variable, but only for functions that have **kwargs:

  def spam(a, b, **kwargs):
      kwargs = OrderedDict((k, kwargs[k]) for k in __kwargs_order__)

My gut feeling is that option 2 is the better choice.  And before you
ask, Guido has already vetoed making **kwargs always be an
OrderedDict.  There are a few other details to work out, but the above
is the main thing.

Also, an OrderedDict implemented in C will likely be a prerequisite,
but luckily I've had a patch up since July(?) (#16991).  It doesn't
apply cleanly at present, but I expect that's only barely and I should
be able to get a clean patch up when I have a chance.  There wasn't a
lot of interest in reviewing such a large patch so I'd back-burnered
it until Python 3.4 got released (which just happened).  Once I get a
couple reviews I should be able to get that in (wink wink nudge


More information about the Python-ideas mailing list