3 ms·
In PHP, the "if ($a = 1)" and "if ($a == 1)" look a lot like each others. In the Python version, the new "(a := 1)" will stand out compare to the canonical "a
by sametmax 8y ago
In PHP, the "if ($a = 1)" and "if ($a == 1)" look a lot like each others.
In the Python version, the new "(a := 1)" will stand out compare to the canonical "a == 1" as the ':' and '()' are unexpected here, and scream that it's not regular if test.
- dvlsg 8y agoSame problem in Javascript. There's actually a yoda lint rule that encourages you to write your code like (1 === a) instead, so you don't accidentally make that mistake.
- makapuf 8y agoI hate Yoda assignment in C. Does no good when assigning to variables. I think it's better to use the weird (on purpose) ((x=13)) and forbid x=13
- korijn 8y agoI'm curious to see how it will pan out in practise, but I'm not getting my hopes up. I made a flake8 plugin to forbid assignment expressions: https://github.com/Korijn/flake8-assignexp https://github.com/Korijn/flake8-assignexp Will be released once python 3.8 is out and I can test it.
- geoelectric 8y agoThat seems unnecessarily reactionary, but ok. I agree with the post you're responding to that having a completely separate token for inline assignment fixes the majority of issues that have historically come up with it (which mostly come down to forgetting an = in the ==). Might be worth feeling out the trajectory of the feature before proactively banning it from codebases you control.
- korijn 8y agoYou're right. Like I said, I'm curious to see how it will pan out, but I'm not getting my hopes up. I'm just not a fan of this new syntax. :) IMHO null-safe navigation operators like ?. and ?[] are much more important to have.
- thomasahle 8y agoThe '()'s are not always needed though, as in the PEP examples `(y := f(x), x/y)` and `(x := 0, x := 1)`. The alternative `f(x) as y` syntax looked nice to my eyes, and doesn't introduce new symbols. However I'm sure they are right, that it would have bad corner cases.