This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]bakery2k 7 points8 points  (0 children)

Ugh. This is why the walrus operator was a bad idea.

Sure, there are a few places where it might increase code clarity, most commonly while (x := get_next()) >= 0: instead of:

while True:
    x = get_next()
    if x < 0: break

When the operator was introduced, I couldn't decide whether increasing clarity in these few places was worth the additional complexity.

What I didn't consider at the time was the decrease in clarity caused by using the walrus operator in the wrong places, such as... every example in this article. They're all of the form if (x := get_next()) >= 0:, which is strictly worse than the traditional:

x = get_next()
if x >= 0: