DEV Community

Discussion on: Python SnakeBytes: The Walrus Operator

 
codemouse92 profile image
Jason C. McDonald • Edited

List comprehensions are pretty normal for some pure-functional languages, like Haskell, but they're rare (and thus feel "advanced") for coders more familiar with, say, C++ or Java.

(The snark definitely wasn't called for.)

Thread Thread
 
renegadecoder94 profile image
Jeremy Grifski

Agreed! But, there's more to my example than just a list comprehension.

Thread Thread
 
codemouse92 profile image
Jason C. McDonald • Edited

Oh, definitely. I think that one would actually make a few of my Python colleagues cringe, with the walrus operator being in it like that...but then, the walrus is controversial to begin with!

Thread Thread
 
renegadecoder94 profile image
Jeremy Grifski • Edited

For sure! I was wondering why they would include the feature because I feel like it goes against Python's design a little bit. For one, it's an expression and a statement which makes it a little ambiguous. Of course, I'm glad that they used a different operator for it.

That said, I just found a really nice example (source):

while chunk := file.read(8192):
  process(chunk)

Now, that's clean!