4 ms·
Some of these newer features don't seem like improvements to the language (e.g. the Walrus operator): ''' # ===== Don't write this ===== response = get_user_
by smjburton 1y ago
Some of these newer features don't seem like improvements to the language (e.g. the Walrus operator):
'''
# ===== Don't write this =====
response = get_user_input()
if response:
print('You pressed:', response)
else:
print('You pressed nothing')
# ===== Write this instead =====
if response := get_user_input():
print('You pressed:', response)
else:
print('You pressed nothing')
'''
The first implementation is immediately clear, even if you're not familiar with Python syntax. If you don't know what the ":=" operator does, the code becomes less readable, and code clarity is traded away in favor of being slightly more concise.
- edwardjxli 1y agoIn that example I gave, I totally agree with you. One area where I find walrus operators kinda useful is with dealing with iterators. For example, ''' iterable = iter(thing) while val := next(iterable, None): print(val) ''' is a lot cleaner in my opinion compared to ''' iterable = iter(thing) val = next(iterable, None) while val is not None: print(val) val = next(iterable, None) ''' Reason why I did not use this example outright was because I wasn't sure if people were familiar with the iter api, so I just chose a simpler example for the blog.
- maleldil 1y agoIsn't this equivalent to just `for val in thing`?
- sgarland 1y agoIf `thing` already supports iteration, then yes, but sometimes you need to build your own [0]. [0]: https://docs.python.org/3/library/functions.html#iter https://docs.python.org/3/library/functions.html#iter
- maleldil 1y agoParent's example had a direct `iter(thing)`. Even if you had to use `iter(thing, sentinel)`, you'd still use `for val in iter(thing, sentinel)`, not while.
- pansa2 1y agoYeah, I think the walrus operator is occasionally useful in `while` statements, but that's about it. IMO it wasn't worth the additional language complexity for an operator that's so rarely useful.
- Izkata 1y agoYou can get code formatting by indenting with 2 spaces: https://news.ycombinator.com/formatdoc https://news.ycombinator.com/formatdoc
- maleldil 1y agoI 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 1y 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 1y 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 1y 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 1y 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.
- frde_me 1y agoCouldn't you extend this line of thinking to any language-specific syntax on any programming language? Don't use `match`, macros, lifetimes, ... in rust, someone coming from another language without them might not get what it means. Instead write the equivalent C-looking code and don't take advantage of any rust specific things. Don't use lisp, someone coming from another language might not be able to read it. Etc.. At one point if you write code and want to be productive, you need to accept that maybe someone that is not familiar with the language you're using _might_ have to look up syntax to understand what's going on.
- smjburton 1y agoOne of the main objectives of Python is readability. IMO, features that take away from this objective become a detriment to the language.