Python-list Info Page

Gary Robinson grobinson at
Sun Oct 31 04:43:40 CET 2004

I am trying to understand a couple of nuances of inheriting from the 
tuple type.

The following class definition works nicely:

class Test(tuple):
    def __init__(self, tup):
        self.x = 5
        self = tup        
a = Test((1,2))
print a, a.x

The result of that print is "(1, 2), 5". 

But the following version:

class Test(tuple):
    def __init__(self, tup):
        self = tup
        self.x = 5
a = Test((1,2))

blows up in Python 2.3, complaining that attribute x doesn't exist.

I'm not clear on why one works and not the other, or whether there's a 
better syntax for assigning an attribute to a tuple subclass in the 
__init__() method.

Anyone have any  thoughts??


Gary Robinson
Transpose, LLC
grobinson at

More information about the Python-list mailing list