[Python-ideas] Different interface for namedtuple?

Carl M. Johnson cmjohnson.mailinglist at gmail.com
Fri Mar 4 08:17:09 CET 2011

I've been following the discussion on the list about decorators for
assignments and whatnot, and while I do think it would be interesting
if there were some way to abstract what happens in the creation of
classes and defs into a more generalizable structure, for the
immediate use case of NamedTuple, wouldn't an interface something like
this solve the DRY problem? --

class Point(NewNamedTuple): x, y

This interface couldn't have worked in Python 2.6 when namedtuple was
introduced, but using the __prepare__ statement in Python 3, it's
trivial to implement. As a bonus, NewNamedTuple could also support
type checking with the following syntax:

class Point(NewNamedTuple): x, y = int, int #or whatever the ABC is
for int-like numbers

In fact, since as it stands namedtuple is called "namedtuple" in
lowercase, we could just camel case "NamedTuple" to collections
instead of using the NewNamedTuple or whatever. Or, and I haven't done
any research on this, it may also be possible to implement
NewNamedTuple with backwards compatibility so it can also be used in
the traditional way.

-- Carl Johnson

More information about the Python-ideas mailing list