non-copy slices

Terry Reedy tjreedy at udel.edu
Wed Nov 18 19:49:27 CET 2009


tbourden at doc.ic.ac.uk wrote:
> Hi,
> 
> I was looking for a facility similar to slices in python library that 
> would avoid the implicit creation of a new list and copy of elements 
> that is the default behaviour. Instead I'd rather have a lazy iteratable 
> object on the original sequence. Well, in the end I wrote it myself but 
> I was wondering if I missed sth in the library  If I didn't is there a
> particular reason there isn't sth like that? I find it hard to believe 
> that all slice needs have strictly copy semantics.

It is a strict *shallow* copy. There is no copying of contents.
That aside, you are right, hence itertools.islice as already mentioned. 
In the design of 3.0, I believe the idea was raised of making slices 
iterables in 3.0, just as was done for map, filter, and range. However, 
it would have been highly disruptive, and not save much space. Map and 
range create an unbounded number of new objects, rather than just a 
sequence of references to existing objects (or bytes or words for bytes 
and str slices). There is also the problem of virtual slices preventing 
garbage collection of the source sequence when it is not otherwise needed.

Terry Jan Reedy




More information about the Python-list mailing list