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

Nick Coghlan ncoghlan at gmail.com
Wed Jul 26 11:48:52 EDT 2017


On 26 July 2017 at 03:49, MRAB <python at mrabarnett.plus.com> wrote:
> I don't see how they could be compatible with tuples because the positions
> aren't fixed. You would need a NamedTuple where the type specifies the
> order.

Python 3.6+ guarantees that keyword-argument order is preserved in the
namespace passed to the called function.

This means that a function that only accepts **kwargs can reliably
check the order of arguments used in the call, and hence tell the
difference between:

    ntuple(x=1, y=2)
    ntuple(y=1, x=2)

So because these are implicitly typed, you *can't* put the arguments
in an arbitrary order - you have to put them in the desired field
order, or you're going to accidentally define a different type.

If that possibility bothers someone and they want to avoid it, then
the solution is straightforward: predefine an explicit type, and use
that instead of an implicitly defined one.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-ideas mailing list