<div dir="ltr"><div><div><div><div><div><div><div>(Attempting to reply to <a href="https://mail.python.org/pipermail/python-ideas/2014-April/027443.html">https://mail.python.org/pipermail/python-ideas/2014-April/027443.html</a>)<br>
<br>> In JS, if you want to define a simple object, you can write it in two ways:<br>> {'spam': 'ham'}<br>> or<br>> {spam: 'ham'}<br>> But when you have a key name defined in a variable, you'll need to do<br>
> key = 'spam'<br>> o = {}<br>> o[key] = 'ham'<br>> Where in Python, you'd simply write {key: 'ham'}.<br>> So for Python, I think that having unquoted keys in literals is a bad idea.<br>
<br></div>I agree with this (even if you no longer do). There's no precedence (that I can think of) for NOT evaluating things before a colon, and nothing like using attr names (rather than objects that hold attribute names) with a colon.<br>
<br></div>As long as we're just fooling around with ideas, here's an alternative proposal for namedtuple literal syntax[0]. I don't mean this as an actual suggestion, but I think it is better than unquoted keys before colons.<br>
</div> ('color':c, 'position':c) #quoted, or with variables<br> (color=c, position=c) #unquoted, with literals<br></div>Example:<br></div> p = (x=1, y=5) #type is something new, like `ntuple`, or just `namedtuple`.<br>
</div><br>This is similar to keyword arguments, which don't have to quote their first parts. This can be generalized to dicts, to keep consistency.<br> d = {'hello':1, 'world':2} #original<br> d = {hello=1, world=2} #new<br>
</div><div>and the earlier-proposed "ordered dict literal"[1]<br> od = ['hello':1, 'world':2] #new<br><div></div> od = [hello=1, world=2] #new new<br></div><div><br></div><div>Or alternatively, with `=` just appearing in ntuples, this can be used:<br>
</div><div> od = OrderedDict((hello=1, world=2))<br></div><div>which is interesting and also somehow horrifying.<br></div><div><br></div><div>Some arguments against this:<br></div><div>- Introducing `=` in yet another context. Ugly.<br>
</div><div>- Tuples are ordered, and **kwargs are unordered[2], so there's a conceptual discontinuity there.<br>- Accidentally writing `:` when you mean `=` can lead to silent bugs.<br></div><div>- Extending the syntax to dictionaries possibly violates TOOWTDI, and not doing so feels inconsistent to me. I think about `(var_holding_spam: 1, literally_eggs=2)` and I'm like, "This is obviously two ways of doing it."<br>
</div><div>- People might expect `p = (x=1) to be a 1-ntuple. I don't want it to be, and I don't like that it wouldn't be.<br></div><br><div>I like the taste of the syntax (and the generalizations, and the OrderedDict literal), but I see how it isn't necessarily necessary (or welcome).<br>
<br></div><div>[0] Found a mention of this here: <a href="https://mail.python.org/pipermail/python-ideas/2010-October/008489.html">https://mail.python.org/pipermail/python-ideas/2010-October/008489.html</a><br></div><div>
[1] Guido hated that: <a href="https://mail.python.org/pipermail/python-ideas/2009-June/004924.html">https://mail.python.org/pipermail/python-ideas/2009-June/004924.html</a><br>[2] Making it ordered is obstructed by concerns about performance (<a href="https://mail.python.org/pipermail/python-ideas/2013-February/019699.html">https://mail.python.org/pipermail/python-ideas/2013-February/019699.html</a>). Currently being discussed here: <a href="https://mail.python.org/pipermail/python-ideas/2014-April/027454.html">https://mail.python.org/pipermail/python-ideas/2014-April/027454.html</a><br>
</div></div>