Tuple slices

Terry Reedy tjreedy at udel.edu
Mon Jan 24 17:18:45 EST 2005


"George Sakkis" <gsakkis at rutgers.edu> wrote in message 
news:35kn4mF4o44ufU1 at individual.net...
> Why does slicing a tuple returns a new tuple instead of a
> view of the existing one, given that
> tuples are immutable ? I ended up writing a custom
> ImmutableSequence class that does this, but I
> wonder why it is not implemented for tuples.

Numpy and Numarray both do this -- generate views into arrays -- because 
they are specifically aimed at large datasets for which the memory savings 
overrides the complications.

Aside from the problem of not being able to delete the underlying object, 
the view object for a tuple would have to be a new type of object with a 
new set of methods.  So there would be one more thing to learn.  And so 
'type(o) is tuple' would generally have to be replaced by 
'isinstance(type(o), (tuple,tupleview)).

For slices that are used within expressions and then discarded,
a= (1,2,3)
b = a(1:) + a(:1)
an internal tuple view might be an interesting optimization.

Terry J. Reedy






More information about the Python-list mailing list