[Python-ideas] namedtuple literals [Was: RE a new namedtuple]

Greg Ewing greg.ewing at canterbury.ac.nz
Tue Jul 25 19:58:44 EDT 2017


Nick Coghlan wrote:

> New builtin:
> 
>     ntuple(x=1, y=0)

Do we really want this to be a tuple, with ordered fields?
If so, what determines the order? If it's the order of the
keyword arguments, this means that

    ntuple(x=1, y=0)

and

    ntuple(y=0, x=1)

would give objects with different behaviour.

This goes against the usual expectation that keyword arguments
of a constructor can be written in any order. That's one of
the main benefits of using keyword arguments, that you don't
have to remember a specific order for them.

If we're going to have such a type, I suggest making it a
pure named-fields object without any tuple aspects. In which
case "ntuple" wouldn't be the right name for it, and something
like "record" or "struct" would be better.

Also, building a whole type object for each combination of
fields seems like overkill to me. Why not have just one type
of object with an attribute referencing a name-to-slot
mapping?

-- 
Greg


More information about the Python-ideas mailing list