[Python-ideas] return value of yield expressions

H. Krishnan hetchkay at gmail.com
Tue Sep 13 05:24:47 CEST 2011


Hi,
I should start off by saying that my knowledge of PEP342 is less than
a week old, and I cannot claim any use-case for what I am about to
propose.

The PEP itself says "In effect, a yield-expression is like an inverted
function call; the argument to yield is in fact returned (yielded)
from the currently executing function, and the "return value" of yield
is the argument passed in via send()."

One could, I guess, wrap a function to be 'called' via this mechanism:
def genwrap(func):
   def gen():
       ret = None
       while True:
           args, kwds = (yield ret)
           ret = func(*args, **kwds)
           del args, kwds
   g = gen()
   g.next()
   return g

f = genwrap(func)
f.send( (args, kwds) )

However, 'send' takes only one argument (and hence the poor syntax in
the last statement).
Has the option of the return value of the yield expression treated
very much like function arguments been discussed?

(a1, a2, a3 = default, *args, **kwds) = (yield ret)

'send' could support positional and keyword arguments.

I guess this particular form would break backward compatibility but
there might be other alternatives.

Regards,
Krishnan



More information about the Python-ideas mailing list