Ordered Dictionary Literals
Hey All, We were exploring new features of Python 3.0 at our Tucson User's Group here in Tucson (TuPLE: Tucson Python Language Enthusiasts), in particular, the OrderedDict. See http://groups.google.com/group/TuPLEgroup/browse_thread/thread/40af73f8e194a... Has there been any discussion about making a "better" OrderedDict literal? I did some googling and didn't find anything. Basically, the thought was there might be a place for a slightly better literal for OrderedDict in Python 3.0 od = OrderedDict([('a',1),('b':2)]) # seems clumsy The two ideas floated were: od = ['a':1, 'b':2, 'c':3] # [ ] imply order, the ':' implies key-value or od = o{'a':1, 'b':2, 'c':3} # { } imply dict, o implies order Apologies if this is the wrong place for this discussion. There has been a lot of opinions flying here at work and at TuPLE which I will be happy to share if this is the right place. ;) Gooday, Richie
On 11 Nov 2009, at 17:33 , Richard Saunders wrote:
Hey All,
We were exploring new features of Python 3.0 at our Tucson User's Group here in Tucson (TuPLE: Tucson Python Language Enthusiasts), in particular, the OrderedDict. See
http://groups.google.com/group/TuPLEgroup/browse_thread/thread/40af73f8e194a...
Has there been any discussion about making a "better" OrderedDict literal? I did some googling and didn't find anything.
Basically, the thought was there might be a place for a slightly better literal for OrderedDict in Python 3.0 od = OrderedDict([('a',1),('b':2)]) # seems clumsy
The two ideas floated were: od = ['a':1, 'b':2, 'c':3] # [ ] imply order, the ':' implies key-value
or
od = o{'a':1, 'b':2, 'c':3} # { } imply dict, o implies order
Apologies if this is the wrong place for this discussion. There has been a lot of opinions flying here at work and at TuPLE which I will be happy to share if this is the right place. ;)
The first one is, I think, pretty smart considering the built-in set syntax in 3.x is the same as the dict's, except without the colons ({1, 2, 3} is a set, {'a':1, 'b':2, 'c':3}). Sadly (for the proposal) since PEP 3003 on the Python Language Moratorium (http://www.python.org/dev/peps/pep-3003/) was accepted there can be no change to the language's syntax until the end of the moratorium, and this would be a syntactic alteration of the language. Your only option is therefore to stash it until the end of the moratorium (maybe take the time to try it out/implement it, and submit a full PEP with a ready-made implementation when the moratorium ends). Anyway, if you choose to take the time to implement a proof of concept during the moratorium, I personally prefer the first idea to the second one.
Richard Saunders wrote:
Hey All,
We were exploring new features of Python 3.0 at our Tucson User's Group
You should actually be using 3.1 if you are not.
The two ideas floated were: od = ['a':1, 'b':2, 'c':3] # [ ] imply order, the ':' implies key-value
Interesting idea, but this would mean making ordered dict a fundamental builtin type that all implementations must include rather than a somewhat optional module import. I do not think it qualifies, at least not yet.
or
od = o{'a':1, 'b':2, 'c':3} # { } imply dict, o implies order
Apologies if this is the wrong place for this discussion.
Perfect place for such idea. Terry Jan Reedy
Terry Reedy wrote:
Richard Saunders wrote:
Hey All,
We were exploring new features of Python 3.0 at our Tucson User's Group
You should actually be using 3.1 if you are not.
The two ideas floated were: od = ['a':1, 'b':2, 'c':3] # [ ] imply order, the ':' implies key-value
Interesting idea, but this would mean making ordered dict a fundamental builtin type that all implementations must include rather than a somewhat optional module import. I do not think it qualifies, at least not yet.
or
od = o{'a':1, 'b':2, 'c':3} # { } imply dict, o implies order
Apologies if this is the wrong place for this discussion.
Perfect place for such idea.
This was discussed in mid June. Guido said -100.
2009/11/11 MRAB:
This was discussed in mid June. Guido said -100.
Indeed. See http://mail.python.org/pipermail/python-ideas/2009-June/thread.html#4916 and http://mail.python.org/pipermail/python-ideas/2009-June/004924.html
Richard Saunders wrote:
Has there been any discussion about making a "better" OrderedDict literal? I did some googling and didn't find anything.
I think we want to see it get some more field testing as part of the collections module before bringing it into the core of the language is seriously considered. If it's popular and useful, then doing so is definitely a possibility though. The history of the set builtin (i.e. first introduced in a module, then made a builtin, then given literal syntax) is indicative of the kind of time frames we're talking about here. Rather than immediately jumping to a literal though, it might prove to be more fruitful to explore the use of an ordered dictionary for keyword arguments. Without that, convenient shortcuts like "OrderedDict(a=1, b=2, c=3)" would never become possible. Cheers, Nick. -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia ---------------------------------------------------------------
Richard Saunders <richismyname@...> writes:
Basically, the thought was there might be a place for a slightly better literal for OrderedDict in Python 3.0 od = OrderedDict([('a',1),('b':2)]) # seems clumsy
How about something like: od = OrderedDict.from_literal(""" {'a': 1, 'b': 2} """) Of course, you need to hook/reimplement a full-blown parser :)
On 11/12/2009 05:46 PM, Antoine Pitrou wrote:
Richard Saunders <richismyname@...> writes:
Basically, the thought was there might be a place for a slightly better literal for OrderedDict in Python 3.0 od = OrderedDict([('a',1),('b':2)]) # seems clumsy
How about something like:
od = OrderedDict.from_literal(""" {'a': 1, 'b': 2} """)
Of course, you need to hook/reimplement a full-blown parser :)
this would eliminate the [ ]: def odict(*items): return OrderedDict(items) od = odict(('a', 1), ('b', 2)) well, 2 chars isn't much. however, I don't think its worth the effort.
2009/11/12 Mathias Panzenböck <grosser.meister.morti@gmx.net>:
On 11/12/2009 05:46 PM, Antoine Pitrou wrote:
Richard Saunders <richismyname@...> writes:
Basically, the thought was there might be a place for a slightly better literal for OrderedDict in Python 3.0 od = OrderedDict([('a',1),('b':2)]) # seems clumsy
How about something like:
od = OrderedDict.from_literal(""" {'a': 1, 'b': 2} """)
Of course, you need to hook/reimplement a full-blown parser :)
this would eliminate the [ ]: def odict(*items): return OrderedDict(items)
od = odict(('a', 1), ('b', 2))
well, 2 chars isn't much. however, I don't think its worth the effort.
Or: def odict(*items): iteritems = iter(items) return OrderedDict(zip(items, items)) od = odict('a',1, 'b',2) Not worth it either :) -- Arnaud
participants (9)
-
Antoine Pitrou
-
Arnaud Delobelle
-
Carl M. Johnson
-
Masklinn
-
Mathias Panzenböck
-
MRAB
-
Nick Coghlan
-
Richard Saunders
-
Terry Reedy