[Python-Dev] (name := expression) doesn't fit the narrative of PEP 20
Steven D'Aprano
steve at pearwood.info
Sat Apr 28 04:33:47 EDT 2018
On Fri, Apr 27, 2018 at 04:24:35PM -0400, Wes Turner wrote:
> if ((1) or (x := 3)):
> if ((y := func(x)) if x else (x := 3))
Wes, there is *absolutely nothing new* here. This sort of error is
already possible in Python.
Do you see a lot of code like:
if (1 or sequence.append(3) or sequence[-1]):
in real life? If you do, then I'm really, really sorry that you are
forced to deal with such rubbish code, but honestly, the vast bulk of
Python programmers do not write like that, and they won't write this
either:
if (1 or (x := 3)):
[...]
> Assignment expressions, though they are noticeable :=, may not actually
> define the variable in cases where that part of the line doesn't run but
> reads as covered.
The same applies to any operation at all.
/sarcasm
I guess adding print() to the language was a mistake, because we
can write rubbish code like this:
if 1 or print(x):
and then be confused by the fact that x doesn't get printed.
/end sarcasm
In another post, you insisted that we need to warn in the PEP and the
docs not to do this sort of thing. Should we also go through and add
these warnings to list.append, dict.update, set.add, etc?
I trust that the answer to that is obviously no.
And neither do we have to assume that people who use binding-expressions
will lose their minds and start writing the sort of awful code that
they don't write with anything else.
--
Steve
More information about the Python-Dev
mailing list