[Python-ideas] Ordered Dictionary Literals

4 views
Skip to first unread message

Richard Saunders

unread,
Nov 11, 2009, 11:33:30 AM11/11/09
to python...@python.org

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/40af73f8e194a4f8

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

Masklinn

unread,
Nov 11, 2009, 11:44:12 AM11/11/09
to Richard Saunders, python...@python.org

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.
_______________________________________________
Python-ideas mailing list
Python...@python.org
http://mail.python.org/mailman/listinfo/python-ideas

Terry Reedy

unread,
Nov 11, 2009, 2:09:44 PM11/11/09
to python...@python.org
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

MRAB

unread,
Nov 11, 2009, 2:39:37 PM11/11/09
to python...@python.org
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.

Nick Coghlan

unread,
Nov 11, 2009, 4:13:07 PM11/11/09
to Richard Saunders, python...@python.org
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 | ncog...@gmail.com | Brisbane, Australia
---------------------------------------------------------------

Carl M. Johnson

unread,
Nov 11, 2009, 11:43:13 PM11/11/09
to python...@python.org

Antoine Pitrou

unread,
Nov 12, 2009, 11:46:26 AM11/12/09
to python...@python.org
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 :)

Mathias Panzenböck

unread,
Nov 12, 2009, 4:27:40 PM11/12/09
to python...@python.org
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.

Arnaud Delobelle

unread,
Nov 13, 2009, 5:07:26 AM11/13/09
to Mathias Panzenböck, python...@python.org
2009/11/12 Mathias Panzenböck <grosser.me...@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

Reply all
Reply to author
Forward
0 new messages