3 ms·
I disagree. First of all, it takes a minute to search "python :=", and the construct itself is pretty simple. It's been part of the language since 2018[0]. I d
by maleldil 2y ago
I disagree.
First of all, it takes a minute to search "python :=", and the construct itself is pretty simple. It's been part of the language since 2018[0]. I don't think "not knowing the language" is a good reason to avoid it.
Second, the walrus operator limits the variable's scope to the conditional, which can reduce certain bugs. It also makes some scenarios (like if/elif chains) clearer.
I recommend checking out the PEP for some real-world examples.
[0] https://peps.python.org/pep-0572/ https://peps.python.org/pep-0572/
Edit: my point about walrus scoping is incorrect. The new variable is function-scoped.
- pansa2 2y ago> the walrus operator limits the variable's scope to the conditional Nope! It's still function-scoped. In Python, walrus or no walrus, the body of a conditional is never a separate scope.
- smjburton 2y agoThat's exactly my point. Having to search for the meaning of the operator at all makes the code less readable. I recommend reading the Zen of Python, which covers the design principles of the language.
- sgarland 2y agoI don’t write code to the level of someone who has just finished Hello, World. This isn’t something esoteric, it’s an extremely basic and useful part of the language. I’ve seen this argument used against multiple languages, and it has never made sense to me. “I don’t want to use windowing functions in SQL, because most people don’t know what they are.” So you’d rather give up an incredibly powerful part of your RDBMS, and dramatically increase the amount of bandwidth consumed by your DB? It’s as if the industry is embracing people who don’t want to read docs.
- smjburton 2y agoWe're discussing Python, not SQL or any other language. Using more arcane elements of a language like Python, which emphasizes readability, often makes your code less understandable, difficult to debug, and harder to maintain. I also suggest you read the Zen of Python for an understanding of the language's design principles.