[Python-ideas] Preserving **kwargs order

Andrew Barnert abarnert at yahoo.com
Fri Apr 4 19:51:26 CEST 2014


On Apr 4, 2014, at 9:04, David Mertz <mertz at gnosis.cx> wrote:

> def myfunc(a, b, *keyvals):
>       od = OrderedDict(keyvals)
>       # ... do other stuff
> 
> Call this like:
> 
>   value = myfunc(foo, bar, ('a',1), ('z',2), ('b',3))

Or, possibly more readably:

    def myfunc(a, b, *keydicts):
        od = OrderedDict((k, v) for keydicts in keydicts for k, v in keydict.items())

    value = myfunc(foo, bar, {'a': 1}, {'z': 2})

Not as clean as passing a single dict literal, or a sequence of keywords, but still clearly a sequence of key-value pairs rather than just a sequence of sequences...


More information about the Python-ideas mailing list