subclassing tuple

Tim Peters at
Mon Feb 11 08:51:27 CET 2002

The difference between lists and tuples here is that tuples are immutable.
This is a distinction that didn't exist at the class level in 2.1, but must
exist in 2.2 in order to make what used to be builtin immutable *types* into
classes instead.

__init__ is too late to influence the base contents of an object of an
immutable type:  the object's memory has already been allocated by the time
__init__ is invoked.  It must be that way, else __init__ couldn't have a
self argument (to have a self, the memory for self must already be set up
and bound to self).

That's why __new__ was introduced in 2.2:  __new__ is, in a sense, lower
level than __init__.  It does not take a "self" argument, because it exists
to do memory allocation and initialization of the object, and for immutable
objects you only get this one shot at memory initialization.

At the C API level, an even finer distinction is possible, allowing to break
__new__ into distinct "allocate raw memory" and "minimally initialize
memory" portions.  You don't have to worry about that distinction at the
Python level, though (it wouldn't make sense there, as there is no "allocate
raw memory" facility available at the Python level).

More information about the Python-list mailing list