[Python-ideas] return value of yield expressions

Stefan Behnel stefan_ml at behnel.de
Tue Sep 13 15:00:59 CEST 2011

Jacob Holm, 13.09.2011 14:32:
> On 2011-09-13 14:21, Stefan Behnel wrote:
>> Jacob Holm, 13.09.2011 13:02:
>>> args, kwds = (yield ret)  # any expression really
>>> (a1, a2, a3, *args), kwds = (lambda a1,a2,a3=default,*args, **kwds:
>>>                                (a1,a2,a3)+args, kwds
>>>                               )(*args, **kwds)
>> Note that recent Python versions support extended argument unpacking, so
>> this works:
>>      a1, *other, a2 = return_some_sequence()
> If you look closer you'll see I am actually using that feature already
> in the code snippet you quoted.

Ah, ok, I thought you were *proposing* to add this. That happens on 
python-ideas a lot more often than you seem to expect.

>> If you use the last value of the returned sequence (such as a tuple) to
>> pass a dict, or if you return a tuple with two arguments (posargs,
>> kwargdict), you basically get what you wanted above.
> basically no.  The suggested "function argument unpacking" includes
> support for default values, and for passing positional arguments by
> name.  Everything that happens when you call a function using (*arg,
> **kwds) really.

Well, I really don't see how this is a wide-spread use case (I certainly 
never stumbled over it), but if you feel like needing it, write a utility 
function that does the unpacking for you in a couple of lines and wrap the 
call with that. I have my doubts that it would make your code much clearer.

Especially default values for keyword dict return values do not appear to 
be any useful to me, given that you'd most likely unpack them one by one 
anyway. So you could just use d.get() with a default argument there, thus 
making it explicit and obvious in your code what is going on.


More information about the Python-ideas mailing list