[Python-ideas] Ordered storage of keyword arguments
ncoghlan at gmail.com
Fri Oct 29 02:54:38 CEST 2010
On Fri, Oct 29, 2010 at 8:03 AM, Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:
> What's the use case for this? One of the reasons that keyword
> arguments are useful is that you don't have to care what order
> you write them in!
The use case is being able to interface naturally with any key-value
API where order matters.
# Create an ordered dictionary (WRONG!)
d = OrderedDictionary(a=1, b=2, c=3) # Order is actually arbitrary due
to unordered kw dict
Another example is an addition made to part of the json API (to accept
an iterable of key-value pairs) to work around exactly this problem.
Basically, if an API accepts an iterable of key-value pairs instead of
a dictionary, it's a case where ordered keyword dictionaries would
likely improve usability.
That said, there are plenty of steps to be taken before the idea of
using ordered dictionaries implicitly anywhere in the interpreter can
even be seriously considered. Step 1 is to come up with a
C-accelerated version of collections.OrderedDictionary, step 2 is to
make it a builtin (odict?), step 3 is to consider using it for class
namespaces and/or for keyword arguments by default, then step 4 would
probably be to switch "dict=odict" and add a
collections.UnorderedDictionary interface to the old dict
implementation. The bar for progression (in terms of acceptable
impacts on speed and memory usage) would get higher with each step
along the path.
Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia
More information about the Python-ideas