|
|
Subscribe / Log in / New account

Python multi-level break and continue

Python multi-level break and continue

Posted Sep 1, 2022 12:42 UTC (Thu) by eru (subscriber, #2753)
In reply to: Python multi-level break and continue by jezuch
Parent article: Python multi-level break and continue

Splitting code into smaller functions is often good practice, but not always. I often find it clearer to have the whole inline, so it can be seen at a glance, instead of lots of small functions used only once. The latter is like reading a text that keeps jumping into footnotes!

As a contribution to the fun syntax bikeshedding, a language I was involved in implementing had a loop roughly like this:

while cond1
    while cond2
    ...
    // Want to break out of both loop levels?
    if satisfied
        break foundIt
   endwhile
endwhile foundIt
 
The break is a jump forward, so it makes sense the label is at the endwhile. Of course this wont work too well in Python that lacks a delimiter to mark the end of the loop, other than decreasing indentation.


to post comments

Python multi-level break and continue

Posted Sep 1, 2022 21:00 UTC (Thu) by bartoc (guest, #124262) [Link]

Strongly agree with this take. If you aren't going to reuse it then it's usually not worth splitting stuff out it's own function. Just let me read the darn code. If the function is doing some well known algorithm maybe it _is_ worth it, but even then sometimes a comment is just as good as a function.

Keeping things inline can make optimization opportunities more clear and make anti-optimizations stick out more, as well.


Copyright © 2025, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds