Remix.run Logo
sbinnee 4 days ago

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.

mdaniel 3 days ago | parent | next [-]

"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...

zahlman 3 days ago | parent | prev | next [-]

I'm still "surprised" (though I've known for years) that GvR and Peters were in favour of the design and co-wrote the PEP (https://peps.python.org/pep-0572/), and that the other author is Chris Angelico who I (also) otherwise greatly respect.

Where it's useful, it tends to just save a line of code or work around other defects (for example, the regex example near the start of the PEP points to either a missing null-coalescing operator — which has been repeatedly proposed and rejected, BTW — or the library's fault in not using the "null object" pattern). It's used twice for example as far as I can tell in all of pip and its vendored dependencies (>100 kloc), and it isn't really doing much in either case (https://github.com/pypa/pip/blob/main/src/pip/_internal/meta... and https://github.com/pypa/pip/blob/main/src/pip/_internal/meta... ; it was also used in `typing_extensions` but that's been dropped as a dependency; there are some false-positives related to code that parses source code and some data formats).

But most importantly to me, it fundamentally violates the strong https://en.wikipedia.org/wiki/Command%E2%80%93query_separati... seen throughout the rest of the language.

Meanwhile, changes that I think might actually be good are nowadays routinely rejected with the same kinds of arguments that PEP 572 faced and survived.

It's unfortunate that Guido felt harassed and ended up leaving the project. It was overall better under his leadership. I wish I'd been paying attention at the time. I suspect that the people who created that feeling must have themselves felt betrayed.

mixmastamyk 3 days ago | parent | prev | next [-]

The functionality is sound, but Python already had the “as” pattern available. So I still believe creating a third way to do assignment was a mistake.

tretiy3 3 days ago | parent | prev [-]

i did not get it until learning rust. and only then u realized that: if let Some(msg) = read_message(&mut stream) { is the same as if msg := read_message(stream):