From humble beginnings at an MSP, I've adventured through life as a sysadmin, into an engineer, and finally landed as a developer focused on fixing problems with automation.
I teach computer science to undergrads and write for The Renegade Coder. I'm most likely taking care of my daughter, watching the Penguins, or reading manga.
Location
Columbus, Ohio
Education
B.S. in CE from CWRU 2016; M.S. in CSE from OSU 2020; PhD in EED from OSU 2024
I'm not sure your example really convinces me that this is a good practice (or a good additional feature for that matter). In fact, it's basically the same example given in the article. What's the advantage of placing the assignment in the condition?
To address your snark, there's a mix of features here which is why I called it advanced:
List Comprehension
Walrus Operator
Type Flexibility (result could be some falsey value)
These aren't exactly loops and conditionals we're talking about. The average person isn't going to be able to understand this line just by looking at it.
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.
I teach computer science to undergrads and write for The Renegade Coder. I'm most likely taking care of my daughter, watching the Penguins, or reading manga.
Location
Columbus, Ohio
Education
B.S. in CE from CWRU 2016; M.S. in CSE from OSU 2020; PhD in EED from OSU 2024
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!
I teach computer science to undergrads and write for The Renegade Coder. I'm most likely taking care of my daughter, watching the Penguins, or reading manga.
Location
Columbus, Ohio
Education
B.S. in CE from CWRU 2016; M.S. in CSE from OSU 2020; PhD in EED from OSU 2024
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):
whilechunk:=file.read(8192):process(chunk)
Now, that's clean!
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
It is of great convenience taken from the C languages. I sometimes utilize a similar construct in PowerShell:
Not sure I'd call a list comprehension "advanced"
I'm not sure your example really convinces me that this is a good practice (or a good additional feature for that matter). In fact, it's basically the same example given in the article. What's the advantage of placing the assignment in the condition?
To address your snark, there's a mix of features here which is why I called it advanced:
These aren't exactly loops and conditionals we're talking about. The average person isn't going to be able to understand this line just by looking at it.
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.)
Agreed! But, there's more to my example than just a list comprehension.
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!
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):
Now, that's clean!