[Python-ideas] A subclassing API for named tuples?
Jan Kaliszewski
zuo at chopin.edu.pl
Mon Feb 18 00:45:15 CET 2013
17.02.2013 22:38, Steven D'Aprano wrote:
> On 17/02/13 00:46, Jan Kaliszewski wrote:
>> 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'
>
>
> This implies that all namedtuples must be callable.
No, not at all. It only means that signature specification of the
namedtuple factory
function would need to be extended a bit (or a separate decorator, such
as namedtuple.subclass,
would need to be added as an attribute) to support returning a
decorator instead of
a ready named tuple type.
> Point3D = namedtuple('Point3D', 'x y z')
> pt = Point3D(2, 4, 8)
>
> I don't think it's appropriate for tuples, named or not, to be
> callable.
I don't understand what do you mean. It's obvious that a named tuple
*type* must be callable
(as any other instantiable type) and that in 99% of cases named tuple
*instances* should not be...
Cheers.
*j
More information about the Python-ideas
mailing list