[Python-ideas] Propagating StopIteration value
storchaka at gmail.com
Tue Oct 9 18:18:09 CEST 2012
On 09.10.12 01:44, Guido van Rossum wrote:
> I don't understand that code at all, and it seems to be undocumented
> (no docstrings, no mention in the external docs). Why is it using
> StopIteration at all? There isn't an iterator or generator in sight.
> AFAICT it should just use a different exception.
I agree with you. StopIteration is not needed here (or I don't
understand that code), ValueError can be raised instead it. Perhaps the
author was going to use it for the iterative parsing.
This is a bad example, but it is only real example which I have. I have
also the artificial (but also imperfect) example:
while not f.eof:
d = zlib.decompressobj()
while not d.eof:
yield d.decompress(d.unconsumed_tail or next(input))
decomp = bz2.BZ2Decompressor()
while not decomp.eof:
data = b''
while len(data) < 5:
data += next(input)
decompressor = zlib_decompressor
data = data[5:]
decompressor = bzip2_decompressor
data = data[5:]
decompressor = None
input = itertools.chain([data], input)
return decompressor, input
decompressor, input = detect_decompressor(input)
if decompressor is None:
unused_data = yield from decompressor(input)
if not unused_data:
input = itertools.chain([unused_data], input)
Of cause this can be implemented without generators, using a class to
hold a state.
> I think you're going at this from the wrong direction. You shouldn't
> be using this feature in circumstances where you're at all likely to
> run into this "problem".
I think that the new language features (as returning value from
generators/iterators) will generated new methods of solving problems.
And for these new methods will be useful to expand the existing tools.
But now I see that it is still too early to talk about it.
> Itertools is for iterators, and all the extra generator
> features make no sense for it.
As said Greg, the question is whether returning a value with
StopIteration is part of the iterator protocol or the generator protocol.
More information about the Python-ideas