[Python-ideas] Deterministic iterator cleanup
Todd
toddrjen at gmail.com
Wed Oct 19 15:21:41 EDT 2016
On Wed, Oct 19, 2016 at 2:38 PM, Paul Moore <p.f.moore at gmail.com> wrote:
> 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
>
I may very well be misunderstanding the purpose of the proposal, but that
is not how I saw it being used. I thought of it being used to clean up
things that happened in the loop, rather than clean up the iterator
itself. This would allow the iterator to manage events that occurred in
the body of the loop. So it would be more like this scenario:
>>> lst = objiterer([obj1, obj2, obj3, obj4])
>>> it = iter(lst)
>>> for i, _ in zip(it, [1, 2]):
... b = i.some_method()
>>> for i in it:
... c = i.other_method()
>>>
In this case, objiterer would do some cleanup related to obj1 and obj2 in
the first loop and some cleanup related to obj3 and obj4 in the second
loop. There would be no backwards-compatibility break, the method would be
purely opt-in and most typical iterators wouldn't need it.
However, in this case perhaps it might be better to have some method that
is called after every loop, no matter how the loop is terminated (break,
continue, return). This would allow the cleanup to be done every loop
rather than just at the end.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20161019/c2e362c0/attachment.html>
More information about the Python-ideas
mailing list