[Python-Dev] PEP 380 (yield from a subgenerator) comments

P.J. Eby pje at telecommunity.com
Thu Mar 26 22:16:58 CET 2009

At 12:20 PM 3/26/2009 -0700, Guido van Rossum wrote:
>By brittle I meant again having to be aware of those details of the
>mechanism that exist because of syntactic limitations, e.g.
>accidentally writing "return X" instead of "yield Return(X)".

In that case, you'd either have a syntax error under the current 
rules (because you're using yield in the same function), or else 
you'd have a normal function (no yields) that worked in the way you 
expect it to.

IIRC, the example I gave would treat a non-Return(), non-generator 
value as a value to be passed back into the current generator, such 
that if you defined a function f, and did:

      g = yield f()

and f was not a generator or did not return one, then the above is 
equivalent to:

      g = f()

and is not an error.  Granted, this can fail if f() can return some 
other sort of iterator, but arguably that's an error in the *caller*, 
which should not use yield to call such a function -- and the need to 
distinguish the type of function you're calling is still present in 
PEP 380, i.e., you still need to know whether the function you're 
calling is a generator with a special return mechanism.

More information about the Python-Dev mailing list