[Python-Dev] Proposal / Questions about OrderedDict literals and/or faster C implementation

Steve Goss steveth45 at gmail.com
Thu Feb 10 21:30:02 CET 2011


I have a proposal for a literal syntax for OrderedDicts which is just
replacing the braces with square brackets:

['a': 1,'b': 2] == OrderedDict([('a', 1),('b', 2)])

OrderedDict literals would follow:

[x : x for x in foo()] == OrderedDict([(x,x) for x in foo()])

The rationale for the syntax is that it follows the set / list /
dict precedent of curly braces for unordered collections, square brackets
for ordered collections, and otherwise aping the normal dict syntax.

OrderedDict is arguable one of the best recent additions to the Python
standard library. Looking at the py3k codebase, it seems like adding
OrderedDicts as a native C implementation at the same time as introducing a
literal syntax with all the additions to the grammar and ast makes sense. A
native implementation would make the memory usage closer to normal dicts
(plus two pointers per element) but be potentially faster for many
operations than even regular dictionaries and certainly much, much faster
than the current Python-only implementation of OrderedDict.

I've started working on this in my free time, but I'm not a seasoned CPython
hacker. Any feedback or pointers would be helpful. Originally, I was
planning on creating a patch first before suggesting this to the mailing
list, but given the scope of the feature and the number of concerns I figure
I might as well test the waters first.

Even if the idea of a literal syntax is dismissed, I think a C
implementation of OrderedDict would be a great addition and I'd love to
help.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20110210/b4bebc12/attachment.html>


More information about the Python-Dev mailing list