[Python-ideas] A subclassing API for named tuples?

Antoine Pitrou solipsis at pitrou.net
Thu Feb 14 23:40:08 CET 2013


On Fri, 15 Feb 2013 09:09:57 +1100
Steven D'Aprano <steve at pearwood.info> wrote:

> On 15/02/13 00:19, Nick Coghlan wrote:
> > An exchange with Antoine in one of the enum threads sparked a thought.
> >
> > A recurring suggestion for collections.namedtuple is that it would be
> > nice to be able to define them like this (as it not only avoids having
> > to repeat the class name, but also allows them to play nicely with
> > pickle and other name-based reference mechanisms):
> >
> >      class MyTuple(collections.NamedTuple):
> >          __fields__ = "a b c d e".split()
> 
> 
> How would that differ from this?
> 
> class MyTuple(collections.namedtuple("MyTupleParent", "a b c d e")):
>      pass
> 
> 
> Apart from the DRY violation in the class name, I find that perfectly
> acceptable, and it seems to work fine with pickling:

Well, it's perfectly acceptable as long as you have one-letter field
names. Try with real field names and it becomes rather unwieldy.

Regards

Antoine.





More information about the Python-ideas mailing list