subclassing tuple

Douglas Zongker dougz at
Tue Sep 17 16:05:02 EDT 2002

Is there any way to create a subclass of 'tuple' that has a customized
constructor?  I tried doing the obvious thing, but it doesn't work --
it seems the tuple initializer gets called *before* the one for my
subclass.  This:

    class Foo(tuple):
        def __init__( self, a ):
            print 'self is already:', self
            print 'argument is', repr(a)

    x = Foo('hello')


    value is already set: ('h', 'e', 'l', 'l', 'o')
    argument is 'hello'

I could do this with lists instead, but that means changing a lot of
existing code that expects tuples (and I don't want these objects to
be mutable!)

Suggestions welcome.


More information about the Python-list mailing list