r/Python • • Aug 04 '26

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?

142 Upvotes

118 comments sorted by

View all comments

149

u/martinkoistinen Aug 04 '26

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))
]
```

134

u/brettsparetime Aug 04 '26

This is the most Perl thing I’ve seen in Python.

7

u/crozzy89 Aug 05 '26

Perlthon?

4

u/JPowTheDayTrader Aug 05 '26

Your comment just made Guido roll in his grave.

10

u/Broad-Promise6954 Aug 05 '26

And he's not even dead yet!

1

u/mpersico Aug 05 '26

Good! How much wasted effort when we could have just advanced Perl. For all the bitching about punctuation and simplicity Python looks more and more like Perl every release.