[Python-Dev] PEP 342 suggestion: start(), __call__() and unwind_call() methods

James Y Knight foom at fuhm.net
Sun Oct 9 04:54:10 CEST 2005


On Oct 8, 2005, at 9:10 PM, Nick Coghlan wrote:
> So, instead of having "return" automatically map to "raise  
> StopIteration"
> inside generators, I'd like to suggest we keep it illegal to use  
> "return"
> inside a generator

Only one issue with that: it's _not currently illegal_ to use return  
inside a generator. From the view of the outsider, it currently  
effectively does currently map to "raise StopIteration". But not on  
the inside, just like you'd expect to happen. The only proposed  
change to the semantics is to also allow a value to be provided with  
the return.

>    def generator():
>       yield
>       try:
>          return
>       except StopIteration:
>          print "But we would get here!"

 >>> def generator():
...  yield 5
...  try:
...   return
...  except StopIteration:
...   print "But we would get here!"
...
 >>> x=generator()
 >>> x.next()
5
 >>> x.next()
Traceback (most recent call last):
   File "<stdin>", line 1, in ?
StopIteration
 >>>


James



More information about the Python-Dev mailing list