[Python-ideas] Collect **kw arguments as an ordered dictionary

spir denis.spir at free.fr
Sat Apr 18 14:44:09 CEST 2009

Le Fri, 17 Apr 2009 15:09:51 -0700,
"Raymond Hettinger" <python at rcn.com> s'exprima ainsi:

> [Michael Foord]
> > It would be nice if **kw arguments collected in a function / method
> > signature were collected as an ordered dictionary rather  than ordinary
> > dictionary.
> I think that would be nice, but until ordered dicts get a C implementation,
> it might greatly impair performance.

What is/are the main issue(s) about that?

> > Main use case, currently (I believe) the odict constructor doesn't
> > guarantee to preserve ordering if created with keyword arguments: 
> That is correct.  And so noted in the docs:
>      http://docs.python.org/dev/library/collections.html#ordereddict-objects

More generally, no custom func/constructor can rely on **kwargs. Any order preserving application needs to require (name,value) pair lists instead of the (much nicer imo) kwarg syntax. This is especially pitiful for custom types.

> > My use case - I'd like to preserve the ordering to reproduce exactly the 
> > order of arguments for the Mock module representation of the objects 
> > are used. Because the order of iterating over arguments isn't the same 
> > as they are passed in representations can't be compared in tests 
> > reliably across Python versions / implementations.
> Sounds reasonable.

If only for printed feedback -- first of all to the developper itself -- I think this a sensible feature.

> Raymond

I take the opportunity to talk about something related. This is a data type that would be a kind of list/dict mix, maybe more, but basically ordered. Just discovered that Lua's table (http://lua-users.org/wiki/TablesTutorial) is this kind of thing; but iteration doesn't preserve order (background hash like for python dicts) for non-index keys.

Having very few CS knowledge, I wonder what kind of underlying data structure would easily allow this. As of know, I'm thinking at the following:
* Keys could be anything "stringifiable", serializable or representable as an array of ints.
* An issue is avoiding conflict between keys of != types, eg 1 vs '1'.
* This would allow using trie algorithms (http://en.wikipedia.org/wiki/Trie), especially for item insertion/deletion/update/lookup. Even better may be a Patricia or a de la Briandais tree. Iteration is not an issue precisely because order is kept: it's walking the tree leaves.
* Time efficiency is not my primary concern -- I just don't want it to be stupidly slow ;-)
* I'm also interested in alternatives to implement basically ordered dicts, without any standard python dict in the background. And in (name:value) ordered collections as for namespace registers, or kwarg lists (cf Michael's need above).

Anyone interested to cooperate?
[Aside python itself, my motivation is rather the fun of exploration, and a possible toy custom language I have in mind.]

la vita e estrany

More information about the Python-ideas mailing list