Problem subclassing tuple

Michael Chermside mcherm at mcherm.com
Wed Apr 30 12:35:02 EDT 2003


John Wilson writes:
[This doesn't work:
> class Holder(tuple):
>     def __init__(self, *values):
>         tuple.__init__(self, values)

    [...]

> class Holder(tuple):
>     def __new__(cls, *args, **kargs):
>         return tuple.__new__(cls, args)

> is the way to do it for a tuple :))))))

   [...]

> I'm really quite interested in why this doesn't work. I *thought* I'd
> grasped the concept of the new style classes but this seems to demonstrate
> that I haven't!

Actually, you've discovered the original reason for the invention
of __new__. Guido invented __new__ because __init__ wouldn't work
for classes like tuple that were immutable. The combination of __new__
and __init__ is what is called "two stage initialization" -- the 
__new__ method is the "factory", that just creates the empty object,
while __init__ is the "initializer" that sets up the object. Normally,
you want to override __init__ -- the only exceptions are those cases
where you NEED __new__. For instance, if you want to return objects
of different types depending on some parameters, then you'll need to
use __new__ (or just use a factory function instead of a constructor!).
Or if you want to "initialize" an immutable object, then it'll have
to be done in __new__ (that's your situation).

For more about it, including some discussion of why two stage
initialization is so good, see 
http://cocoa.mamasam.com/MACOSXDEV/2003/03/1/58013.php (and I thought
I'd heard a discussion of the subject in Python recently, but couldn't
find a link).

-- Michael Chermside








More information about the Python-list mailing list