[Python-ideas] PEP 572: Assignment Expressions (post #4)

Chris Angelico rosuav at gmail.com
Wed Apr 11 17:44:15 EDT 2018


On Thu, Apr 12, 2018 at 3:49 AM, Brendan Barnwell <brenbarn at brenbarn.net> wrote:
> On 2018-04-11 05:23, Clint Hepner wrote:
>>
>> I find the assignments make it difficult to pick out what the final
>> expression looks like.
>
>
>         I strongly agree with this, and for me I think this is enough to
> push me to -1 on the whole proposal.  For me the classic example case is
> still the quadratic formula type of thing:
>
> x1, x2 = (-b + sqrt(b**2 - 4*a*c))/2, (-b - sqrt(b**2 - 4*a*c))/2
>
>         It just doesn't seem worth it to me to create an expression-level
> assignment unless it can make things like this not just less verbose but at
> the same time more readable.  I don't consider this more readable:
>
> x1, x2 = (-b + sqrt(D := b**2 - 4*a*c)))/2, (-b - sqrt(D))/2
>
> . . . because having to put the assignment inline creates a visual
> asymmetry, when for me the entire goal of an expression-level statement is
> to make the symmetry between such things MORE obvious.  I want to be able to
> write:
>
> x1, x2 = (-b + sqrt(D)))/2, (-b - sqrt(D))/2 ...
>
> . . . where "..." stands for "the part of the expression where I define the
> variables I'm re-using in multiple places in the expression".

What if you want to use it THREE times?

roots = [((-b + sqrt(D))/2/a, (-b - sqrt(D))/2/a) for a,b,c in
triangles if (D := b**2 - 4*a*c) >= 0]

Now it's matching again, without any language changes. (I've
reinstated the omitted division by 'a', in case anyone's confused by
the translation. It has no bearing on the PEP discussion.) Same if
you're using an if statement.

>         The new proposal does at least have the advantage that it would help
> with things like this:
>
> while x := some_function_call():
>         # do stuff
>
>         So maybe I'm -0.5 rather than -1.  But it's not just that this
> proposal "could be used to create ugly code".  It's that using it for
> expression-internal assignments WILL create ugly code, and there's no way to
> avoid it.  I just don't see how this proposal provides any way to make
> things like the quadratic formula example above MORE readable.

I don't think it's as terrible as you're saying. You've picked a
specific example that is ugly; okay. This new syntax is not meant to
*replace* normal assignment, but to complement it. There are times
when it's much better to use the existing syntax.

ChrisA


More information about the Python-ideas mailing list