[Python-ideas] return value of yield expressions

Guido van Rossum guido at python.org
Tue Sep 13 20:53:14 CEST 2011


On Tue, Sep 13, 2011 at 11:35 AM, Jacob Holm <jh at improva.dk> wrote:
> The '*(argument list) = <expression>' syntax was my attempt at fixing
> some of the problems in the original proposal.  I.e. to remove the
> restriction that it only should work with "= yield", and distinguish the
> "function argument unpacking" from the regular tuple unpacking.
>
> My idea was to allow
>
>  *(argument list) = (args, kwds)
>
> where the LHS follows all the rules of a function argument list (the
> part of a function definition between the name and the colon), and
> the RHS may be any expression that returns a 2-tuple consisting of a
> sequence and a dictionary.
>
> The intended semantics was to compute the arguments that a function with
> the given argument list would see when called with (*args, **kwds), and
> make the necessary assignments in the local scope.  Any way I can think
> of that works today violates DRY.
>
>
> A simple example:
>
> *(a, b, c=42, *args, **kwds) = ((1,), {'b':2, 'd':3})
>
> assigns a=1, b=2, c=42, args=(), kwds={'d':3}

Thanks, I finally understand. Part of the reason why it was unclear to
me was that I was reading the form inside *(...) as having the same
priority rules as a regular assignment, where "," binds tighter than
"="; whereas in a function argument list "=" binds tighter than ",".

> And my best hack so far to get the same effect today is:
>
> args, kwds = ((1,), {'b':2, 'd':3})
> a, b, c, args, kwds = \
>  (lambda a, b, c=42, *args, **kwds:a, b, c, args, kwds)(*args, **kwds)
>
>
> I am not wed to the particular suggested syntax, but I would like to see
> the functionality available *somehow* in a way where you don't have to
> repeat the sequence of variable names.  And I don't think that is
> possible without *some* syntax change.

I agree that you can't do this today.

But so what?

I am not so convinced that DRY is always the most important rule to invoke.

Apart from the extremely esoteric example of wanting to call g.send()
with a mix of positional and keyword arguments that are somehow
interpreted as a completely general function parameter list by the
receiving yield expression, what is the use case?

-- 
--Guido van Rossum (python.org/~guido)



More information about the Python-ideas mailing list