[Python-ideas] Deterministic iterator cleanup
Steven D'Aprano
steve at pearwood.info
Fri Oct 21 07:23:52 EDT 2016
On Fri, Oct 21, 2016 at 11:03:51AM +0100, Paul Moore wrote:
> At the moment, the take home message for such users feels like it's
> "you might need to scatter preserve() around your code, to avoid the
> behaviour change described above, which you glazed over because it
> talked about all that coroutiney stuff you don't understand" :-)
I now believe that's not necessarily the case. I think that the message
should be:
- If your iterator class has a __del__ or close method, then you need
to read up on __(a)iterclose__.
- If you iterate over open files twice, then all you need to remember is
that the file will be closed when you exit the first loop. To avoid
that auto-closing behaviour, use itertools.preserve().
- Iterating over lists, strings, tuples, dicts, etc. won't change, since
they don't have __del__ or close() methods.
I think that covers all the cases the average Python code will care
about.
--
Steve
More information about the Python-ideas
mailing list