[issue20663] Introduce exception argument to iter

Josh Rosenberg report at bugs.python.org
Fri Jul 4 00:02:32 CEST 2014


Josh Rosenberg added the comment:

+1; I've had several cases where I'd have used something like this (for the exact purpose mentioned, to destructively consume an input iterable). I don't think it's more or less useful than the sentinel version, which is convenient for iterating a file by blocks instead of by line, e.g.:

from functools import partial

with open('...', 'rb') as f:
    for block in iter(partial(f.read, 4096), b''):
        ...

But it would still nice to be able to destructively iterate sequences, particularly in CPython, where doing it at the C level can get you atomicity without relying on anything beyond the GIL (and without wrapping infinite while loops in try/except: pass blocks, which is pointlessly verbose).

One issue: You can't just make the second argument allow exception types as well, since it's possible (however unlikely) for an exception type to be a legitimate return type from a function. Making it keyword only would solve that problem though.

----------
nosy: +josh.rosenberg

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue20663>
_______________________________________


More information about the Python-bugs-list mailing list