0

Is the walrus Operator antipattern?

I just stumbled upon the Walrus Operator (:=), https://www.pynerds.com/walrus-operator-in-JUMP_LINK__&&__python__&&__JUMP_LINK/ I was startled by how such a syntax could exist in python when they literally do not allow the ++ operator which is equally ugly. Do you think the walrus operator is antipattern?, I can't imagine a scenario where it can solely make sense without making code hard to read. Why not just use multiple assignments?. huh. See demos: https://www.pynerds.com/visualize/?code=14bdc68e-34a5-4e36-9037-153e998f927e https://www.pynerds.com/compiler/?code=14bdc68e-34a5-4e36-9037-153e998f927e

24th Mar 2025, 4:28 AM
John Maina
John Maina - avatar
4 Answers
+ 6
The walrus operator can be needed in some cases. It makes the program shorter and easier with less efforts. Python does not supports ++ operator because it is designed to be a more explicit and readable language. The += operator helps making more sense.
24th Mar 2025, 5:22 AM
Unknown Decoder
Unknown Decoder - avatar
+ 4
Walrus operator is a "nice to have" feature IMO. You can write code without it, and no one force you to use it. It gives you more options to complete a job. And use it or not is your call.
24th Mar 2025, 6:50 AM
Wong Hei Ming
Wong Hei Ming - avatar
+ 4
John Maina , from my point of view the walrus operator (assignment expression operator) is not an antipattern. in the attached file you can find 2 code samples. one is doing the task *without* the walrus operator, the other one is *with* walrus operator. see more details in the file. https://sololearn.com/compiler-playground/cj3y75tAsNg2/?ref=app
24th Mar 2025, 11:39 AM
Lothar
Lothar - avatar
+ 2
I could remember walrus was part of the late 2019/2020 update to python. It was such a nice life changer for oneliner like me
24th Mar 2025, 5:38 PM
RuntimeTerror
RuntimeTerror - avatar