[Python-ideas] Deterministic iterator cleanup

Ethan Furman ethan at stoneleaf.us
Wed Oct 19 15:11:40 EDT 2016


On 10/19/2016 11:38 AM, Paul Moore wrote:

> 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.

Agreed.  I like the idea in general, but this particular break feels like a deal-breaker.

I'd be okay with not having break close the iterator, and either introducing a 'break_and_close' type of keyword or some other way of signalling that we will not be using the iterator any more so go ahead and close it.  Does that invalidate, or take away most of value of, the proposal?

--
~Ethan~


More information about the Python-ideas mailing list