|
|
Subscribe / Log in / New account

Python multi-level break and continue

Python multi-level break and continue

Posted Sep 19, 2022 12:04 UTC (Mon) by sammythesnake (guest, #17693)
In reply to: Python multi-level break and continue by dtlin
Parent article: Python multi-level break and continue

I actually really like this - to my mind it answers all the qualms I've had and AFAICS those others have expressed (based on the article/comments here - I've not read the threads)

It's easy to read, nicely encourages clear naming/labeling (at least in a much as a syntactic requirement to *have* a label), requires no extra charges to the language...

The only niggle is that the syntax of having a method call serve the purpose of a flow control construct is a *little" klunky...

It's even the kind of thing that might make a good library/module, even if an unusually small one :-P - perhaps a candidate to be included in an already existing standard library module (¿itertools?)

Some thoughts on how it could be expanded - more hands-holding support for more styles of loops, such as specific buttons for while/until/for/whatever. Perhaps parameters to the init of the Loop class would be the place to do that: "with Loop(while=<expr>): ..." or something.

Perhaps a future possibility would be syntactic support to adding this kind of context manager to other loops (like somebody above suggested using the "with" keyword) though that might end up with duplicate functionality if the context manager has already implemented features for specific loop like concepts, so that might be something to decide on sooner rather than later...

I'd also use a base class and child classes for the exception because a Boolean doesn't give much scope for distinguishing varieties of flow control conditions and there might be further future additions here (Possibly a retry the current loop option, for example...?)


to post comments


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