[Python-ideas] return value of yield expressions
Jacob Holm
jh at improva.dk
Tue Sep 13 16:17:08 CEST 2011
On 2011-09-13 15:00, Stefan Behnel wrote:
> Jacob Holm, 13.09.2011 14:32:
>>> 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.
I remember running into this exact problem at one point when working
with decorators, but don't remember the details. IIRC you really can't
do much in terms of writing a helper function. You can get far, but not
all the way.
a, b, c, args, kwds = helper(
(lambda a, b, c=3, *args, **kwds:locals()),
(args, kwds)
)
is IIRC the best you can do. You need the tuple-unpacking assignment to
get the names into locals, and you need an actual function (the lambda)
to specify the arguments in a natural way. You still end up repeating
the names of each argument, which is a DRY violation.
No helper function can get you even close to the readability and
non-DRYness of
*(a, b, c=42, *args, **kwds) = (args, kwds)
>
> 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.
The point is to be able to get from a (args, kwds) tuple to some actual
locally assigned names, based on the rules we already know and love from
function arguments. I know this is a weak argument, based on purity
rather than practicality, but until I run into the issue again I really
can't give you an example.
- Jacob
More information about the Python-ideas
mailing list