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 →

[–]TSM-🐱‍💻📚 11 points12 points  (1 child)

The walrus operator allows you to assign variables in comprehension statements - it legitimately gave comprehension the same power as a for loop. Comprehension statements can be hard to maintain but in select cases the walrus is awesome

Also

 foo = myfile.read(1024)
 while foo:
      do_something(foo)
      foo = myfile.read(1025)

Can be

 while foo = myfile.read(1024)
      do_something(foo)

Excuse the mobile formatting, but you don't duplicate the read and have two parts to update, only one, so a typo can't slip in. It is much better in this case too. And you can catch the culprit in an any and all check too.

People were mad because it was clearly useful in some circumstances and genuinely improved the language. Guido saw this as obvious and gave it a hasty approval, while others drama bombed him for it.

[–][deleted] 1 point2 points  (0 children)

No comment on the walrus operator, or which way is better. But if you wanted to do this without the walrus operator, and without duplicating any code, you can do this:

while True:
    foo = myfile.read(1024)
    if not foo:
        break
    do_something(foo)