[Python-ideas] Fwd: Anonymous namedtuples

Joseph Martinot-Lagarde contrebasse at gmail.com
Tue Apr 19 08:30:10 EDT 2016

Chris Angelico <rosuav at ...> writes:

> On Tue, Apr 19, 2016 at 9:31 PM, Joseph Martinot-Lagarde
> <contrebasse at ...> wrote:
> > Chris Angelico <rosuav <at> ...> writes:
> >
> >>
> >> On Tue, Apr 19, 2016 at 7:49 PM, Joseph Martinot-Lagarde
> >> <contrebasse <at> ...> wrote:
> >> > I'd be happy to make a factory function for my personal use, but the
> >> > of kwargs is not respected. To have an elegant syntax, it has to be a
> >> > construct of the language.
> >>
> >> There have been proposals to have kwargs retain some order (either by
> >> having it actually be an OrderedDict, or by changing the native dict
> >> type to retain order under fairly restricted circumstances). With
> >> that, you could craft the factory function easily.
> >
> > I saw some proposals but forgot about them, thanks for the reminder.
> >
> > I'd prefer my proposal for two reasons:
> > - the syntax is nicer that a factory function
> > - it wouldn't be possible to add keyword arguments to __getitem__ without
> > breaking compatibility
> >
> > Those issues are relatively minor, i'd be happy with a factory function
> Agreed, the syntax is a lot nicer - for this specific situation. Don't
> forget, though, that every piece of new syntax carries with it the not
> insignificant cost of complexity; and some things are better
> represented by function calls than syntax (cf 'print'). With "from
> types import SimpleNamespace as NS" at the top of your code, you can
> use the keyword-arguments form of the constructor to be almost the
> same as your proposal, without any new syntax. When a subsequent
> maintainer looks at your code, s/he can quickly look up at the imports
> to see what's going on, instead of having to learn another piece of
> syntax.

I agree on the principle, but to me this syntax looks less complex to me
compared to the actual namedtuple where you have to use a factory to create
a class to be able to use a namedtuple. And if you use a namedtuple as a
return value, the definition of the namedtuple class will typically be
relatively far away from the actual use of it.

SimpleNamespace is nice but would break compatibility for return values.

> I have to agree. Fortunately it isn't hard to create a namedtuple factory:
As you said before, it would be if the order of the keyword arguments was

> def NS(fields, *values):
>     return namedtuple('anonymous', fields.split())(*values)
> That cuts down the duplication some, but it's far from perfect.

It cuts down the duplication at the cost of readability.

There is also the Matlab way which looks a bit better:

    from collections import namedtuple

    def NS(*args):
        fields = args[::2]
        values = args[1::2]
        return namedtuple('anonymous', fields)(*values)

    print(NS("x", 12, "y", 15))

But readability counts, and it's very easy to introduce bugs as adding or
removing arguments can impact the whole chain. Initially coming from Matlab,
the greatest interest I found in python was named arguments for functions! :)

More information about the Python-ideas mailing list