New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Doc for itertools, 'islice()' implementation have unwanted behavior for recipe 'consume()' #71399
Comments
In the doc for itertools, the python implementation of islice() doesn't actually perform the iteration when given an empty slice (e.g. islice(it,3,3)). This is not the expected behavior for 'consume()' in the recipe given below, i.e. consume(it,3) couldn't actually advance the iterator for 3 steps ahead. Instead, the iterator stays at its starting position. As a simple fix, in the implementation of islice(), the length of the slice object should be checked first. If it's zero, the iterator should be advanced (slice.start-1) times, and return (See islice_my.py) |
The recipe should be modified to consume the initial values. Something like this:
The recipe needs to pass these tests: it = iter('abcdefghi')
assert list(islice(it, 4, 4)) == []
assert list(it) == ['e', 'f', 'g', 'h', 'i']
assert(list(islice('ABCDEFG', 2)) == ['A', 'B'])
assert(list(islice('ABCDEFG', 2, 4)) == ['C', 'D'])
assert(list(islice('ABCDEFG', 2, None)) == ['C', 'D', 'E', 'F', 'G'])
assert(list(islice('ABCDEFG', 0, None, 2)) == ['A', 'C', 'E', 'G']) |
Hi Nofar, Are you still interested in working on this issue? Thanks! |
Cheryl, I think you take this one. It hasn't been touched in a long time and Nofar is focused on another issue. |
Thanks, Raymond. I'll take a look. |
Thanks Cheryl. This was nice work :-) |
Thanks! :-) |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: