subclassing tuple

Gerhard Häring gh_pythonlist at gmx.de
Mon Feb 11 02:16:06 EST 2002


Le 10/02/02 à 22:43, damien  morton écrivit:
> Ive been trying to subclass the tuple type, without a whole lot of success
> 
> Heres the code Ive been using:
> 
> class triple(tuple):
>     def __init__(self, x, y, z):
>         tuple.__init__(self, (x,y,z))
> 
> >>> t = triple(1,2,3)
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> TypeError: tuple() takes at most 1 argument (3 given)

Well, tuple constructors take only 1 argument :-)

The following works for me, however:

#!/usr/bin/python2.2
class doubledlist(list):
    def __init__(self, x):
        list.__init__(self, map(lambda t: 2*t, x))

x = doubledlist([1,2])
print x

> For some reason, subclasses of tuple dont call the proper __init__ code.

Yes, /me observed this too. Lists work as expected, tuples don't!?

Gerhard
-- 
This sig powered by Python!
Außentemperatur in München: 6.9 °C      Wind: 3.6 m/s




More information about the Python-list mailing list