3 ms·
This has long (always?) been a basic part of the Python language. How do you decide what is a “niche syntax trick”? Here’s the python language reference from p
by jacobolus 8y ago
This has long (always?) been a basic part of the Python language. How do you decide what is a “niche syntax trick”?
Here’s the python language reference from python version 1.4 (1996), http://web.archive.org/web/19970606191447/http://www.python.org:80/doc/ref/ref7.html#HDR2 http://web.archive.org/web/19970606191447/http://www.python....
> The expression list is evaluated once; it should yield a sequence. The suite is then executed once for each item in the sequence, in the order of ascending indices. Each item in turn is assigned to the target list using the standard rules for assignments, and then the suite is executed. When the items are exhausted (which is immediately when the sequence is empty), the suite in the else clause, if present, is executed, and the loop terminates.
> A break statement executed in the first suite terminates the loop without executing the else clause's suite. A continue statement executed in the first suite skips the rest of the suite and continues with the next item, or with the else clause if there was no next item.
* * *
Here’s the tutorial, http://web.archive.org/web/19970606183615/http://www.python.org:80/doc/tut/node31.html http://web.archive.org/web/19970606183615/http://www.python....
> The break statement, like in C, breaks out of the smallest enclosing for or while loop.
> The continue statement, also borrowed from C, continues with the next iteration of the loop.
> Loop statements may have an else clause; it is executed when the loop terminates through exhaustion of the list (with for) or when the condition becomes false (with while), but not when the loop is terminated by a break statement. [... and then an example ...]
- fratlas 8y agoMy point was more towards benwilber0's comment; not a lot of usage in code I've seen.
- daveFNbuck 8y ago> How do you decide what is a “niche syntax trick”? You decide with experience. When other people read my code, this is probably the feature that surprises them most. Other features that they haven't seen before usually make sense from context. This one is surprising enough that I often forget whether it executes if there is a break or if there isn't a break. When explaining and remembering what the syntax means dwarfs the effort saved by using the syntax, it's a niche syntax trick.