Problem subclassing tuple

sismex01 at hebmex.com sismex01 at hebmex.com
Tue Apr 29 11:56:17 EDT 2003


> From: John Wilson [mailto:tug at wilson.co.uk]
> Sent: Tuesday, April 29, 2003 10:55 AM
> 
> Given this class:
> 
> class Holder(tuple):
>     def __init__(self, *values):
>         tuple.__init__(self, values)
> 
> 
> When I try to create an instance:
> 
> Holder(1, 2)
> 
> I get:
> 
> TypeError: tuple() takes at most 1 argument (2 given)
> 
> It looks like the constructor for the superclass is being 
> called rather than the __init__ method on Holder.
> 
> I'm obviously missing something here. Do I have to supply a 
> __new__ method? Any help would be appreciated.
> 
> John Wilson
>

Howdy.

First of all... What are you trying to do? :-)

Second.  If that's the full definition and intention
of "Holder", then you might want to know (or are
overlooking) the fact that "values", inside __init__,
is already a tuple.

Instead of a class Holder, you could build a function
holder() which simply returns the tuple it receives:

def holder(*values):
    return values

One of the most useful functions I've encountered
which simplified my (programming) life in small but
noticeable ways, is the following:

def Dict(**values):
    return values

This allows me to define dictionaries like this:

d = Dict(one=1, two="Two", three=...)

instead of this:

d = { "one":1, "two":"Two", ... }

So...

hth.

-gus





More information about the Python-list mailing list