Problem subclassing tuple
Michele Simionato
mis6 at pitt.edu
Tue Apr 29 17:46:08 EDT 2003
"John Wilson" <tug at wilson.co.uk> wrote in message news:<mailman.1051634628.4864.python-list at python.org>...
> <snip> I want an object which has all the characteristics of a tuple with
> additional methods.
What about this ?
class Holder(tuple):
def additionalmethod(self):
print "ciao ciao"
t=Holder((1,2))
print t #=> (1,2)
t.additionalmethod() #=> ciao ciao
If you want to avoid the parenthesis, here is the solution:
class Holder(tuple):
def __new__(cls,*args):
return tuple.__new__(cls,args)
def additionalmethod(self):
print "ciao ciao"
t=Holder(1,2)
print t # => (1,2)
t.additionalmethod() #=> ciao ciao
Easy, isn't it ?
Michele
More information about the Python-list
mailing list