[Python-Dev] assignment expressions: an alternative proposal
Steven D'Aprano
steve at pearwood.info
Tue Apr 24 11:27:59 EDT 2018
On Tue, Apr 24, 2018 at 11:03:35AM -0400, Yury Selivanov wrote:
> My point was that when you see lots of '=' and ':=' used at the
> statement level, one might try to write "if x = 1" instead of "if x :=
> 1" -- boom, we have an unexpected SyntaxError for some users.
That's a *good* thing. They will then learn not to write x = 1 as an
expression.
Also, if I write lots of x := 1 binding-expressions as statements, my
code is bad and deserves to fail code-review. But why would I write the
extra colon (one character, two key-presses) to use
x := 1
as a statement, when x = 1 will work? That's a sure sign that I don't
know what I'm doing. (Or that I desperately wish I was writing Pascal.)
I don't think we need worry about this. The sort of code that is filled
with binding-expressions used as statements will almost certainly be so
un-Pythonic and ugly in many other ways, that this won't make any
difference.
> In my opinion adding *any* assignment expression syntax to Python
> *will* create this sort of issues. PEP 572 isn't free of them, my
> proposal isn't free of them. My proposal doesn't add a new ':='
> operator at the cost of slightly complicating rules around '='. PEP
> 572 avoids complicating '=', but adds an entirely new form of
> assignment.
Indeed. That is true: either way, we introduce complexity into the
language. (But that will allow us to reduce complexity in *our* code.)
Given that increasing complexity is inevitable regardless of whether we
choose PEP 572 or your suggestion, it is better to choose the option
which keeps binding-expressions and assignment statements separate,
since they are two different concepts.
--
Steve
More information about the Python-Dev
mailing list