On Fri, 15 Feb 2013 09:09:57 +1100
Steven D'Aprano
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.