[Python-ideas] A namedtuple literal.

Franklin? Lee leewangzhong+python at gmail.com
Mon Apr 14 23:44:17 CEST 2014


(Attempting to reply to
https://mail.python.org/pipermail/python-ideas/2014-April/027443.html)

> In JS, if you want to define a simple object, you can write it in two
ways:
>      {'spam': 'ham'}
> or
>      {spam: 'ham'}
> But when you have a key name defined in a variable, you'll need to do
>      key = 'spam'
>      o = {}
>      o[key] = 'ham'
> Where in Python, you'd simply write {key: 'ham'}.
> So for Python, I think that having unquoted keys in literals is a bad
idea.

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.

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.
    ('color':c, 'position':c) #quoted, or with variables
    (color=c, position=c) #unquoted, with literals
Example:
    p = (x=1, y=5) #type is something new, like `ntuple`, or just
`namedtuple`.

This is similar to keyword arguments, which don't have to quote their first
parts. This can be generalized to dicts, to keep consistency.
    d = {'hello':1, 'world':2} #original
    d = {hello=1, world=2} #new
and the earlier-proposed "ordered dict literal"[1]
    od = ['hello':1, 'world':2] #new
    od = [hello=1, world=2] #new new

Or alternatively, with `=` just appearing in ntuples, this can be used:
    od = OrderedDict((hello=1, world=2))
which is interesting and also somehow horrifying.

Some arguments against this:
- Introducing `=` in yet another context. Ugly.
- Tuples are ordered, and **kwargs are unordered[2], so there's a
conceptual discontinuity there.
- Accidentally writing `:` when you mean `=` can lead to silent bugs.
- 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."
- 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.

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).

[0] Found a mention of this here:
https://mail.python.org/pipermail/python-ideas/2010-October/008489.html
[1] Guido hated that:
https://mail.python.org/pipermail/python-ideas/2009-June/004924.html
[2] Making it ordered is obstructed by concerns about performance (
https://mail.python.org/pipermail/python-ideas/2013-February/019699.html).
Currently being discussed here:
https://mail.python.org/pipermail/python-ideas/2014-April/027454.html
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140414/fa121e09/attachment.html>


More information about the Python-ideas mailing list