[Python-Dev] PEP 572: Assignment Expressions
Tim Peters
tim.peters at gmail.com
Mon Apr 23 11:57:13 EDT 2018
[Tim]
>> Which this alternative expresses directly:
>>
>> if (diff := x - x_base) and (g := gcd(diff, n)) > 1:
>> return g
>>
>> That's so Pythonic I could cry ;-)
[Antoine]
> It looks like C to me. That won't make me cry (I write C++ code daily
> these days), but it's certainly not the same language as Python.
>
> The second part, especially, where you use the result of an
> assignment expression as a comparison operand, looks definitely
> un-Pythonic.
You snipped the part explaining _what's_ "Pythonic" about it:
It's _really_ an "and" test: if the diff isn't 0 and gcd(diff, n) >
1, return the gcd. That's how I _thought_ of it from the start.
"Expresses directly" is the Pythonic part; the syntax is minor to me.
Seeing that the _intent_ is an "and test" is a pattern-matching puzzle
in the original spelling (which essentially turned me into a compiler,
writing low-level code for the _concepts_ I had in mind from the
start):
diff = x - x_base
if diff:
g = gcd(diff, n)
if g > 1:
return g
But note that the part of the PEP I support is just the "binding
expression" part: giving a simple name (binding an identifier) to the
result of an expression. I don't want the full potential complexity
of assignment statements in expressions. There's nothing
"un-Pythonic" about merely giving a name to an expression result,
apart from that there are few contexts that currently support that in
a sanely usable way.
More information about the Python-Dev
mailing list