[Python-ideas] namedtuple() subclasses again

Daniel da Silva ddasilva at umd.edu
Mon Mar 28 19:11:44 CEST 2011

Is there a use case other an adding __repr__?

The most popular way to use namedtuples are just are just a shorthand for
defining a special type of simple class.

But if you're going to be adding methods, you're breaking out of simple
situation they are used for, and you might as well just free yourself and
make it the class.


On Sun, Mar 27, 2011 at 5:40 PM, Raymond Hettinger <
raymond.hettinger at gmail.com> wrote:

> On Mar 27, 2011, at 1:53 PM, Jan Kaliszewski wrote:
> > Another approach could be a decorator transforming a given class into
> > namedtuple with methods defined in that class:
> >
> >    @namedtuple.from_class
> >    class MyRecord:  # or e.g. class MyRecord(MyMixinWithSomeMethods):
> >        fields = 'username password'
> >        def __str__(self):
> >            return '{0.__class__}({0.username}, ...)'.format(self)
> For the record (pun intended), I'm opposed to changing the API for
> namedtuples.
> It is a mature, successful API that stands to benefit very little from from
> making a second way to do it.
> Experimentation is great and it would be nice to have alternative recipes
> posted in the ASPN Cookbook or some other place, but I believe the standard
> library is the wrong place to fiat in a second way to create them.  If a new
> recipe gains traction, we can link to it from the docs.
> Python development is currently suffering from excess enthusiasm with
> advanced code manipulations occurring upon instantiation -- metaclasses,
> decorators, and context managers are fun to play with, but no fun to debug
> or trace through when something goes wrong.
> Raymond
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20110328/3798f2ef/attachment.html>

More information about the Python-ideas mailing list