[Python-ideas] Different interface for namedtuple?

Carl M. Johnson cmjohnson.mailinglist at gmail.com
Sat Mar 5 05:24:04 CET 2011

I tend to agree that the trouble with my proposed interface is that it
says "class" but you can't do normal class sorts of things like create
methods for the namedtuple subclass. There's also the inelegance that
NamedTuple is treated as a special case by the TupleNamer metaclass,
different from subclasses of NamedTuple. There's a similar issue with
ORMs where normally a subclass of Table is a description of a table
and its fields, but sometimes you want to actually create a new class
that's like Table and you can't get that done simply by subclassing.

A lot of these problems could be addressed by something like the
proposed "make" keyword. Imagine if the interfaces for NamedTuple and
Table were:

make NamedTuple(rename=True) Point:
    x, y

make Table() Author:
    firstname, lastname, DOB = Str(), Str(), Date()

Those strike me as nice enough declarative syntaxes. But working out
exactly how a make statement would work tends to give me a headache. I
suppose at a minimum, a making-object should have a __prepare__ method
and an __init__ method (or perhaps an __enter__ method, and an
__exit__ method). But the more one thinks about all the aspects that
would be nice for a good making-object, the more confusing it

-- Carl Johnson

More information about the Python-ideas mailing list