←back to thread

312 points chmaynard | 1 comments | | HN request time: 0.415s | source
Show context
sbinnee ◴[] No.45062385[source]
I am surprised that the walrus operator had to do with Guido's resignation. The walrus operator is awesome. There are so many cases you need a temporary variable in a control flow. The walrus is a beautiful way to handle it.
replies(4): >>45065832 #>>45065905 #>>45068295 #>>45072135 #
1. mdaniel ◴[] No.45065832[source]
"beautiful" is a bit of a stretch. I am completely open to the fact that doing it in a Pythonic way would make the grammar angry but things have gone off the rails when one needs to introduce a 2nd assignment operator into a language

  if ma = re.search(needle, haystack):
      print("ok, things are sane")
I cannot think of a stellar reason why assignment shouldn't be an expression in sane languages. While digging up the grammar[1] I was also reminded that walrus is seemingly incompatible with type annotations, too

  >>> if (x: str := re.sub("a", "b", "abc")):
    File "<python-input-8>", line 1
      if (x: str := re.sub("a", "b", "abc")):
         ^
  SyntaxError: invalid syntax
Also, you say "temporary variable" but it's no more temporary than any other local variable, because lexical scoping is evidently for children

I have always hated that comprehension variables leak for that same reason

1: https://github.com/python/cpython/blob/v3.13.7/Grammar/pytho...