4 ms·
I hate it when code uses weird niche features that are not obvious, are impossible to look up and add 0 value aside from saving a couple of lines of code.
by grosjona 8y ago
I hate it when code uses weird niche features that are not obvious, are impossible to look up and add 0 value aside from saving a couple of lines of code.
- jkaplowitz 8y agoMe too, but this use case seems clearer than most features I'd label that way, even though I didn't know about it either. It certainly shouldn't be used when unclear, just like any feature. There's no reason this feature's sensible use cases should be as niche as they currently are.
- deathanatos 8y ago> are impossible to look up Questions about Python's syntax are anything but impossible to look up. Python has excellent documentation around this, and the steps to get to the right part of the documentation are pretty straight-forward: 1. Start at the main page, https://docs.python.org/ https://docs.python.org/ 2. Click "Language Reference"; note the subtitle that clearly hints this is what we want: "describes syntax and language elements" 3. We are interested in for loops, so we click "8.3. The for statement"; the resulting page describes the behavior.
- XorNot 8y agoAgreed. The way this ends up looking is absolutely unclear what it does - and seems like more a quirk then a feature.
- vortico 8y ago[Mistake, a moderator please delete this comment! Completely agree with the parent comment now. for...else is dumb and unintuitive. I misunderstood its function, which proves the above point to me.]
- Izkata 8y ago> The symantics of for...else are immediately obvious The code in your example is how it reads, but that's not how it works. The "obvious" reading is wrong, so it's obviously not "immediately obvious". This is what it's actually doing: found = False for l in get_list(): if ...: found = True break if not found: ...
- reificator 8y agoI think it's worth keeping the comment around for that exact reason. It underscores that the concept really is confusing and mislabeled.