4 ms·
>They didn't select my preferred syntax I don't write much python so there's probably something obvious I'm missing, but I don't see why they didn't use "=". I
by Rebelgecko 7y ago
>They didn't select my preferred syntax
I don't write much python so there's probably something obvious I'm missing, but I don't see why they didn't use "=". Is there some significant difference between assignment expressions and assignment statements that makes it worth having distinct syntax?
- jimktrains2 7y agoYes, there is. If x = 1 Has been the source of many errors in many languages. Forcing assignment to be more than a 1 character difference from the equality operator prevents this.
- jorams 7y agoI may be missing something, but := is still only a 1 character difference, isn't it?
- chungy 7y ago:= is basically the "yes, I really mean it" version, whereas bare = might just be a simple typo.
- chrisshroba 7y agoIt is, but many intro programmers (and non-intro programmers!) often forget to use == instead of = for comparison, because in the real world, = more often implies equality, rather than assignment. So a novice might mistakenly type `if x=1:` but they would be unlikely to accidentally type `if x:=1:`.
- madhadron 7y agoMakes you wonder why we switched from = is equality and := is assignment like it was in the ALGOL family.
- jimktrains2 7y agoI should have said single-character additions or deletions . == And = have a levenstein distance of 1, == and := is 2. You can't simply make a typo and change an equality check to assignment. So, wiki defines levenstein distance as including substitution, but I was taught or at least remember it only being additions and deletions?
- ulucs 7y agoIt's the same topology as long as string lengths are equal
- dragonwriter 7y agoIt depends on the definition of edit distance used; they both have a unweighted Levenshtein distance of 1 from “==” (# of inserts, deletions, or substitions), but “:=” has an LCS distance of 2 vs “=” with 1. Perhaps more importantly, “=” and “==” are more visually similar than “==” and “:=” and also easier to mistakenly type for each other.
- joshuamorton 7y agoThe ability to accidently assign when you mean to compare leads to bad bugs. Avoiding "yoda-style" c code in Python is the major reason.
- deleted 7y ago[deleted]
- fooblitzky 7y agoIt seems if you use '=' it leads to dangerous programming bugs[1], whereas ':=' somehow prevents those bugs? [1] https://effbot.org/pyfaq/why-can-t-i-use-an-assignment-in-an-expression.htm https://effbot.org/pyfaq/why-can-t-i-use-an-assignment-in-an...
- mintplant 7y agoIt's much easier to leave off one = when you meant == than it is to accidentally type :=
- dodobirdlord 7y agoIt's so that something like this remains invalid syntax, instead of an unpleasant accident. x = False if x = True: sadness()
- dragonwriter 7y ago> Is there some significant difference between assignment expressions and assignment statements that makes it worth having distinct syntax? Given that Python is statement-oriented, yes, having statements visually distinct from similar expressions is important. It's also important to avoid making the equality operator and the assignment operator visually similar or easy to typo one for the other, which is arguably the bigger need for “:=” vs “=”, since “=” and “==” are quite similar and easy to accidentally mistype for each other.