[Python-ideas] Deterministic iterator cleanup
Paul Moore
p.f.moore at gmail.com
Wed Oct 19 14:38:28 EDT 2016
On 19 October 2016 at 19:13, Chris Angelico <rosuav at gmail.com> wrote:
> Now it *won't* correctly call the end-of-iteration function, because
> there's no 'for' loop. This is going to either (a) require that EVERY
> consumer of an iterator follow this new protocol, or (b) introduce a
> ton of edge cases.
Also, unless I'm misunderstanding the proposal, there's a fairly major
compatibility break. At present we have:
>>> lst = [1,2,3,4]
>>> it = iter(lst)
>>> for i in it:
... if i == 2: break
>>> for i in it:
... print(i)
3
4
>>>
With the proposed behaviour, if I understand it, "it" would be closed
after the first loop, so resuming "it" for the second loop wouldn't
work. Am I right in that? I know there's a proposed itertools function
to bring back the old behaviour, but it's still a compatibility break.
And code like this, that partially consumes an iterator, is not
uncommon.
Paul
More information about the Python-ideas
mailing list