[Python-Dev] PEP 572: Assignment Expressions

Greg Ewing greg.ewing at canterbury.ac.nz
Mon Apr 23 19:54:13 EDT 2018


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?

 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


More information about the Python-Dev mailing list