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

Jan Kaliszewski zuo at chopin.edu.pl
Sat Feb 16 14:46:39 CET 2013


16.02.2013 11:18, Antoine Pitrou wrote:
> On Sat, 16 Feb 2013 15:51:28 +1100
> Steven D'Aprano <steve at pearwood.info> wrote:
>>
>> from collections import namedtuple
>>
>> FIELDNAMES = """..."""  # Format it however you like.
>>
>> class MyClassWithAnExtremelyLongName(namedtuple("Cheese", 
>> FIELDNAMES)):
>>      pass
>
> Still not very elegant IMO

I agree.  I see some nicer (IMHO) alternatives...  Apart from the 
recipe I mentioned
in the recent post (although I am *not* convinced it should be added to 
the stdlib)
some decorator-based way may be nice, e.g.:

     @namedtuple(fields='length weight is_poisonous')
     class Snake:
         def hiss(self):
             return 'hiss' + self.length * 's'

Cheers.
*j




More information about the Python-ideas mailing list