[Python-ideas] Preserving **kwargs order

David Mertz mertz at gnosis.cx
Fri Apr 4 18:04:07 CEST 2014


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.  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.

Even if you have an existing OrderedDict that you want to pass in, you can
use that like:

  value = myfunc(foo, bar, *myOD.items())

Of course, if you want to be picky, you could stick in a check at the top
of your function definition:

  assert all(isinstance(x, tuple) and len(x)==2 for x in keyvals)





-- 
Keeping medicines from the bloodstreams of the sick; food
from the bellies of the hungry; books from the hands of the
uneducated; technology from the underdeveloped; and putting
advocates of freedom in prisons.  Intellectual property is
to the 21st century what the slave trade was to the 16th.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140404/6baebdcf/attachment-0001.html>


More information about the Python-ideas mailing list