[Python-ideas] A namedtuple literal.

Yury Selivanov yselivanov.ml at gmail.com
Wed Apr 2 18:42:32 CEST 2014


On 2014-04-02, 12:34 PM, Philipp A. wrote:
> 2014-04-02 17:20 GMT+02:00 Yury Selivanov <yselivanov.ml at gmail.com>:
>
> 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 totally agree for dicts, but here i don’t. instead i’d require unquoted
> keys like in kwargs.
>
>     1.
>
>     the key here needs to be a valid identifier to be accessed using
>     namedtuple.key_name.
>
>     this could be ensured by the parser if only unquoted keys are allowed,
>     not if string keys or even variable keys containing arbitrary objects are
>     allowed
>      2.
>
>     i don’t actually want this to be something with dynamic keys.
>
>     it’s supposed to be a namedtuple, and namedtuples are static as well
>     regarding their keys.
>
>     i think it’s best to encourage that functions returning namedtuples only
>     return one kind of them, so that you can ensure that returnvalue.my_keyworks.
>
All good points.

Generally, I'm +1 on the idea.

Yury


More information about the Python-ideas mailing list