Re:Making Go telemetry opt-in is a mistake
by Kwpolska on Sunday February 26, 2023 @09:59AM
(#34946028)
The walrus operator did not exist before 3.8, there was no real alternative before. The backlash was from people who dislike the idea and would prefer Python didn't add it.
[ Parent
| on HN ]
Re:Making Go telemetry opt-in is a mistake
by UncleEntity on Sunday February 26, 2023 @10:20AM
(#34946183)
The operator didn’t exist but the functionality did. It was just a side effect of python’s wonky variable scoping which they gave an operator.
I’m not familiar with how it works now but the example I remember was the variables escaping from list comprehension statements, like:
x = [y for y in z]
if y != z[-1]:
bad_stuff_happened()
Or something like that, maybe I’m wrong, honestly never had a use for the walrus.
Re:Making Go telemetry opt-in is a mistake
by Kwpolska on Sunday February 26, 2023 @01:32PM
(#34947991)
Python’s scoping rules are wonky, but the walrus is generally useful in two cases. The first one is to define a variable in an `if` and only run the contents of the `if` if the variable is truey — for example, re.search returns a Match object or None:
if (m := re.search("regex is(n't)? fun")):
print("there was a match")
# do something with m
The other would be defining a variable in the `if` clause of a list comprehension and using it in the output (which is not necessary in languages where map/filter is a first-class citizen, because you could just map before you filter): [y for x in xs if (y := f(x)) == 5]
I can’t think of a way to define a variable in either of those cases without using the walrus and without significantly extending the code (you can define the `m` object and then do `if m:` on two lines, or you can nest two list comprehensions).