←back to thread

2024 points randlet | 1 comments | | HN request time: 0.234s | source
Show context
js2 ◴[] No.17516019[source]
Background ("PEP 572 and decision-making in Python"):

https://lwn.net/Articles/757713/

replies(2): >>17516132 #>>17516693 #
jcelerier ◴[] No.17516132[source]
> The problem with the C-style of assignments is that it leads to this classic error: if(x = 0) {...}

yeah, if you code on 20 years old compilers with no warnings. GCC 4.1 warns about this (with -Wall) and Clang 3.4 warns about this too, without any warning flag.

replies(4): >>17516174 #>>17516220 #>>17517715 #>>17518178 #
jstimpfle ◴[] No.17516220[source]
Not having a problem in the first place is preferable to fighting it with tools. == vs = is a classic mistake that I'm sure every C programmer has wasted some time on. (I'm just undecided if I prefer dealing with this very problem occasionally, or choosing either of the verbosity of := assignments or the non-orthogonality of = assignment statements plus := assignment expressions.)
replies(2): >>17516712 #>>17517362 #
bluecalm ◴[] No.17516712[source]
Well, the best option would be to have := (or any other operator) as C style assignment in the first place. This is huge backward compatibility breakage though so the second best option is to use =.

You can require additional brackets around assignment if you use the returning value (or otherwise it's syntax error). They did that with the new one anyway.

replies(1): >>17517599 #
thomasahle ◴[] No.17517599[source]
> You can require additional brackets around assignment if you use the returning value (or otherwise it's syntax error). They did that with the new one anyway.

They only did it if the := is at the root level. The following is completely legal:

    if match := re.search(pat, text):
        `print("Found:", match.group(0))
or

    [y := f(x), y**2, y**3]
replies(1): >>17518152 #
1. bluecalm ◴[] No.17518152[source]
Great, I haven't used := thing yet. I think it's more elegant like that.