<div dir="ltr">It doesn't feel to me like the existing (non-literal) spelling is particularly bad:<div><br></div><div><div>>>> o1 = OrderedDict(foo=1, bar=2, baz=3)</div><div>>>> o2 = OrderedDict([('foo',1), ('bar',2), ('baz',3)])</div>
<div>>>> o1</div><div>OrderedDict([('bar', 2), ('foo', 1), ('baz', 3)])</div><div>>>> o2</div><div>OrderedDict([('foo', 1), ('bar', 2), ('baz', 3)])</div></div>
<div><br></div><div>So sure, you can't use the kw=val style of initialization, but the "list of pairs" style works fine. It's not that much longer than a literal would be.</div><div class="gmail_extra">
<br><br><div class="gmail_quote">On Tue, Mar 18, 2014 at 5:54 PM, Anthony Towns <span dir="ltr"><<a href="mailto:aj@erisian.com.au" target="_blank">aj@erisian.com.au</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hi,<br>
<br>
I was re-reading some old threads about ordereddict literals to see if<br>
any of them had gotten anywhere. Amongst them, I came across a post by<br>
Tim Delaney:<br>
<br>
<a href="https://mail.python.org/pipermail/python-ideas/2011-January/009049.html" target="_blank">https://mail.python.org/pipermail/python-ideas/2011-January/009049.html</a><br>
<br>
that mentioned an odict literal of ['key': 'value', 'key2': 'value2']<br>
could be confused with slice notation.<br>
<br>
>From a syntax point-of-view, that doesn't seem to be true (as<br>
mentioned in some of the replies to that thread), but it seems like<br>
you can abuse the similarity to make it a little easier to declare<br>
ordereddicts:<br>
<br>
from collections import OrderedDict<br>
class ODSlicer(object):<br>
def __getitem__(self, key):<br>
if type(key) is slice:<br>
key = [key]<br>
od = OrderedDict()<br>
for k in key:<br>
if type(k) is slice:<br>
od[k.start] = k.stop<br>
else:<br>
od[k] = k<br>
return od<br>
od = ODSlicer()<br>
<br>
print(od[1:2])<br>
print(od["a":"b", "c":5])<br>
print(od['a':'b', 'c':'d', ..., 'a':10, 'e':'f'])<br>
<br>
<br>
You could then replace:<br>
<br>
mydict = {<br>
'foo': 'bar',<br>
'baz': 'quux',<br>
}<br>
<br>
with:<br>
<br>
mydict = od[<br>
'foo': 'bar',<br>
'baz': 'quux',<br>
]<br>
<br>
if you need to convert a hardcoded dict into a hardcoded ordereddict.<br>
Works fine in python2.7 and python3.4.<br>
<br>
<br>
At this point, I'd like to note in my defence that this isn't called<br>
the python-good-ideas list :)<br>
<br>
What's the actual objection to supporting ['foo': 'bar'] odict<br>
literals? I saw Guido gave a -100, way back in the day, but no actual<br>
explanation for why it was distasteful?<br>
<br>
<a href="https://mail.python.org/pipermail/python-ideas/2009-June/004924.html" target="_blank">https://mail.python.org/pipermail/python-ideas/2009-June/004924.html</a><br>
<br>
Cheers,<br>
aj<br>
<span class="HOEnZb"><font color="#888888"><br>
--<br>
Anthony Towns <<a href="mailto:aj@erisian.com.au">aj@erisian.com.au</a>><br>
_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" target="_blank">https://mail.python.org/mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" target="_blank">http://python.org/psf/codeofconduct/</a><br>
</font></span></blockquote></div><br><br clear="all"><div><br></div>-- <br>Keeping medicines from the bloodstreams of the sick; food <br>from the bellies of the hungry; books from the hands of the <br>uneducated; technology from the underdeveloped; and putting <br>
advocates of freedom in prisons. Intellectual property is<br>to the 21st century what the slave trade was to the 16th.<br>
</div></div>