5 ms·
I think regex matches might be literally the only use case for := that I come across with any kind of nontrivial frequency, and it's only a minor nuisance at th
by dataflow 5y ago
I think regex matches might be literally the only use case for := that I come across with any kind of nontrivial frequency, and it's only a minor nuisance at that. Certainly nothing to warrant an entirely new yet different syntax for something we already have.
The iterator protocol is way more general than what you have; it's not remotely comparable.
- haihaibye 5y agoI've come to like using: if foo := data.get("foo"): handle_foo(foo)
- pansa2 5y agoThat’s exactly the same as this, though. foo = data.get("foo") if foo: handle_foo(foo) The only place I have found the walrus operator useful is in similar `while` loops, where the equivalent code would be: while True: foo = data.get("foo") if not foo: break handle_foo(foo)
- heavenlyblue 5y agoNo it's not the same. The visibility of variable "foo" is extended beyond it's usefullness if you use `if`.
- pansa2 5y agoNo, the visibility of `foo` is the same in both examples. Python’s `if` statements do not introduce a new scope.
- ptx 5y agoThe loop can be written using This One Weird Trick that Walruses Hate: for foo in iter(partial(data.get, "foo"), None): handle_foo(foo) So I would only use the walrus operator for the first example (the if statement), which even though it is exactly the same as doing it in two steps just feels nicer as a single step.
- slightwinder 5y agoRegex is just a prominent example of a certain pattern. Depending on work and style, one often has functions which return something, on which in case of a non-empty result you want to do something more. Walrus can shrink the code in data-intensive code quite well from my experience.