[Python-ideas] Anonymous namedtuples

Chris Angelico rosuav at gmail.com
Tue Apr 19 06:06:17 EDT 2016


On Tue, Apr 19, 2016 at 7:49 PM, Joseph Martinot-Lagarde
<contrebasse at gmail.com> wrote:
> I'd be happy to make a factory function for my personal use, but the order
> 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.

It may be worth dusting off one of those proposals and seeing if it
can move forward.

That said, though: I don't often feel the yearning for quick
namedtuples. A function that today returns a namedtuple of 'x' and 'y'
can't in the future grow a 'z' without breaking any callers that
unpack "x, y = func()", so extensible return objects have to forego
unpacking (eg using a dict or SimpleNamespace). So you still have a
sharp division between "tuple" and "thing with arbitrary names", and a
namedtuple has to be soundly in the first camp. There *is* a use-case
for this, but it's fairly narrow - you have to have enough names that
you don't want to just unpack them everywhere, yet still have an
order, AND the set of names has to be constant. I suspect the correct
solution here is to make it easier for people to write their own
namedtuple factories, rather than granting them syntactic support.

ChrisA


More information about the Python-ideas mailing list