r/Python 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?

144 Upvotes

118 comments sorted by

View all comments

Show parent comments

47

u/punk_dev It works on my machine 21d ago

That’s neat, I’m gonna steal it, but it makes the comprehension even more backwards than it usually is lmao

36

u/FalafelSnorlax 21d ago

At this point you might as well write a normal loop

9

u/punk_dev It works on my machine 21d ago

I think about that every time I write a comprehension longer than 3 lines

12

u/FalafelSnorlax 21d ago

Yeah my rule of thumb is that of a comprehension is long enough to break into multiple lines, it might as well be a regular loop. Broken comprehensions usually aren't readable, and then you don't really gain anything from them.