[Python-ideas] A sorted version of **kwargs

Tim Delaney timothy.c.delaney at gmail.com
Thu Jan 20 20:47:20 CET 2011

On 21 January 2011 06:13, Bruce Leban <bruce at leapyear.org> wrote:

> Back to the original problem though: if the issue is that creating an
> ordered dict is clumsy and perhaps interfering with adoption and usage then
> perhaps the notation for ordered dict could be improved. Just as we can now
> use {...} for both dicts and sets, perhaps we could add
> [ 'b' : 1, 'a' : 2 ]
> as a more convenient way of writing
> OrderedDict([('b', 1), ('a', 2)])
> This is parallel to the way that [1,2] is an ordered container while {1,2}
> is unordered.

['b':1] would then be ambiguous (appears to be a slice of a list). More
obvious in the case of [1:2] ...

Personally, I'm of the opinion that if an actual dictionary or subclass is
passed via **kw, the same type of dictionary should be used for the keyword
arguments i.e.:

test(**dict(a=1, b=2)) => unordered dict passed.
test(**odict(a=1, b=2)) => ordered dict passed.

The only difficulty then is passing parameters into the ordered dict
constructor in the desired order - I can't think of a reasonable way of
doing that. Good old chicken and egg problem.

Tim Delaney
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20110121/cd813e01/attachment.html>

More information about the Python-ideas mailing list