[Python-Dev] PEP Idea: Syntactic sugar for StopIteration.

Terry Reedy tjreedy at udel.edu
Sat Dec 8 21:59:55 CET 2007


I would prefer plain 'yield' to 'yield break' as a synonym for 'raise 
StopIteration'.
But the raise stands out better visually as an exit point.

The point about the raise not being explicitly caught is not valid since 
any generator could be used in the following code

g = some_gen
try:
    while True:
        process(g.next())
except StopIteration:
    whatever()

where it is caught.  Then under your proposal, there would be a catch 
without a throw ;-(.

If there were a use case for empty generators, then iter() could be made to 
produce one, in analogy with int() returning 0, etc.  But without a use 
case, 'iter()' is much more likely to be a bug and should be flagged as 
such.  In the meanwhile, 'iter(())' is trivial to type.

Terry Jan Reedy





More information about the Python-Dev mailing list