Question about extending tuple
Georg Brandl
g.brandl at gmx.net
Wed Mar 28 15:43:32 EDT 2007
abcd schrieb:
> I wanted to extend tuple but ran into a problem. Here is what I
> thought would work
>
> class MyTuple(tuple):
> def __init__(self, *args):
> tuple.__init__(self, args)
>
> x = MyTuple(1,2,3,4)
>
> That gives me...
>
> TypeError: tuple() takes at most 1 argument (4 given).
>
> However, this call works:
>
> x = MyTuple((1,2,3,4))
>
> I am perplexed only because "args" is a tuple by the definition of
> *args. Anyone?
As an immutable type, tuple makes use of __new__.
class MyTuple(tuple):
def __new__(cls, *args):
return tuple.__new__(cls, args)
should work.
Georg
More information about the Python-list
mailing list