[Python-Dev] PEP 572: Assignment Expressions

Chris Jerdonek chris.jerdonek at gmail.com
Mon Apr 23 21:42:17 EDT 2018


On Mon, Apr 23, 2018 at 4:54 PM, Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:
> Tim Peters wrote:
>
>> if (diff := x - x_base) and (g := gcd(diff, n)) > 1:
>>     return g
>
>
> My problem with this is -- how do you read such code out loud?

It could be--

"if diff, which we let equal x - x_base, and g, which ..." or
"if diff, which we set equal to x - x_base, and g, which ...." or
"if diff, which we define to be x - x_base, and g, which ...." or
"if diff, which we define as x - x_base, and g, which ....." etc.

--Chris



>
> From my Pascal days I'm used to reading ":=" as "becomes". So
> this says:
>
>    "If diff becomes x - base and g becomes gcd(diff, n) is
>     greater than or equal to 1 then return g."
>
> But "diff becomes x - base" is not what we're testing! That
> makes it sound like the result of x - base may or may not
> get assigned to diff, which is not what's happening at all.
>
> The "as" variant makes more sense when you read it as an
> English sentence:
>
>    if ((x - x_base) as diff) and ...
>
>    "If x - x_base (and by the way, I'm going to call that
>     diff so I can refer to it later) is not zero ..."
>
> --
> Greg
>
> _______________________________________________
> Python-Dev mailing list
> Python-Dev at python.org
> https://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe:
> https://mail.python.org/mailman/options/python-dev/chris.jerdonek%40gmail.com


More information about the Python-Dev mailing list