[Python-ideas] return value of yield expressions

Jacob Holm jh at improva.dk
Tue Sep 13 17:57:47 CEST 2011


On 2011-09-13 17:09, H. Krishnan wrote:
> 
> 
>> You need to be able to use "var1 = yield var2" in the generator
>> independent on what send is called with, or you can't wrap a generator
>> using another generator (different ways of doing that was a large part
>> of the PEP380 discussion).
>>
>> So what should "var1" be in each of the following cases?
>>
>> g.send()           # currently illegal
>> g.send(None)       # None
>> g.send((), {})     # currently illegal
>> g.send(((), {}))   # ((), {})
>>
>> I suppose you could change it so passing exactly one argument did
>> something different from passing zero or multiple arguments, but then
>> you have a problem distinguishing when you actually want to use the
>> value in the other end.
>>
> 
> Let us consider from a function point of view:
> If suppose there was no support for *args and **kwds and you wrote:
>   def func(a):
>       do_something_with_a
> and this was called with:
> func((1,2))
> 
> and subsequently, *args, **kwds support was added to functions, will 
> anything related to 'func' need to change?

I think I get your point.  I don't think you are getting mine.

Consider this example:

def foo():
    *(a, b) = yield

f = foo()
next(f)
f.send(1, 2)  # Ok

def wrapper(g):
    r = next(g)
    while 1:
        args = (yield r)
        r = g.send(args) # BOOM!

f = wrapper(foo())
next(f)
f.send(1, 2) # BOOM!


That wrapper works today, and does nothing (except messing up throw
handling, but that is a different story).  With your suggested change,
such a wrapper would break unless it was changed to use

  *(*args, **kwargs) = (yield r)

instead.  IOW a backwards incompatible change.

- Jacob



More information about the Python-ideas mailing list