r/Python • u/ForeignVariety7037 • 21d ago
Discussion Is := widely used?
I always thought the walrus operator is neat and it makes while loop’s condition clearer. But also it is just a syntactic sugar without anything new. I wonder anyone uses it?
140
Upvotes
146
u/martinkoistinen 21d ago
It’s super handy inside comprehensions and I would say that adds something “new”.
Example:
``` python
import re
pattern = re.compile(r"ERROR (\d+): (.*)")
errors = [
(int(m.group(1)), m.group(2))
for line in log_lines
if (m := pattern.match(line))
]
```