[New-bugs-announce] [issue10323] Final state of underlying sequence in islice

Shashank report at bugs.python.org
Fri Nov 5 11:13:39 CET 2010

New submission from Shashank <shashank.sunny.singh at gmail.com>:

-- Converting the discussion here http://mail.python.org/pipermail/python-list/2010-November/1259601.html to a bug report (+nosy for everyone that responded, quoting the initial message for context)

Are there any promises made with regard to final state of the underlying sequence that islice slices?
for example consider this

>>> from itertools import *
>>> c = count()
>>> list(islice(c, 1, 3, 50))
>>> c.next()

Now, the doc [1] says "If stop is None, then iteration continues until the iterator is exhausted, if at all; otherwise, it stops at the specified position".
It clearly is not stopping at stop (3).

Further, the doc gives an example of how this is *equivalent* to a generator defined in the same section. It turns out, these two are not exactly the
same if the side-effect of the code is considered on the underlying sequence.

Redefining islice using the generator function defined in the doc gives different (and from one pov, expected) result
>>> def islice(iterable, *args):
...     # islice('ABCDEFG', 2) --> A B
>>> c = count()
>>> list(islice(c, 1, 3, 50))
>>> c.next()

While "fixing" this should be rather easy in terms of the change in code required it might break any code depending
on this seemingly incorrect behavior

components: Interpreter Core
messages: 120482
nosy: ned.deily, rhettinger, shashank, terry.reedy
priority: normal
severity: normal
status: open
title: Final state of underlying sequence in islice
type: behavior
versions: Python 2.7, Python 3.1, Python 3.2

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list