[Python-ideas] Preserving **kwargs order

Eric Snow ericsnowcurrently at gmail.com
Sat Apr 5 20:49:24 CEST 2014


On Apr 4, 2014 10:04 AM, "David Mertz" <mertz at gnosis.cx> wrote:
> I confess that after reading this thread, and a number of related past
ones, I'm still not certain exactly what problem all of this is needed to
solve.

Yeah, it won't be worth it without justification.  :)  I'll spell it out in
the PEP, but basically it boils down to these reasons:

* Helpful in debugging (from Raymond and others).
* Controlling object presentation.
* Anywhere you want to set iteration order and name/value in a single call
(from Nick).
  + Factories for ordered types.
  + Serializable objects where order matters.
* Keyword argument priority.

I have a few more less concrete use cases as well.  OrderedDict is the
first example everyone cites, but it's simply the most obvious.
Furthermore, it's a rather self-referential example. :)  As I said, I'll
make the full case in the PEP, which I hope to get out today.

> ISTM that if one has a special function calling requirement to pass in an
ordered collection of key/value pairs, one can already just use a special
and available call signature for your function:
>
>   def myfunc(a, b, *keyvals):
>       od = OrderedDict(keyvals)
>       # ... do other stuff
>
> Call this like:
>
>   value = myfunc(foo, bar, ('a',1), ('z',2), ('b',3))
>
> Yes, it's a slightly special form of the calling convention, but it does
something slightly special with its key/val-like arguments, so that seems
like a reasonable tradeoff.  The solution is purely local to the writer of
the function who needs this.

That's clever.  However, it is basically the same as the status quo: if you
want to preserve order you cannot use keyword arguments, including **
unpacking, even though they are the natural way to do it.  For existing
functions, preserving order means changing code even though the order is
already available.  The point of my proposal is to teach the interpreter to
preserve the order.

-eric
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140405/4d5bb1e6/attachment.html>


More information about the Python-ideas mailing list