break in a module

Steven D'Aprano steve+comp.lang.python at pearwood.info
Fri Jun 17 23:50:28 EDT 2011


On Sat, 18 Jun 2011 12:36:42 +1000, Cameron Simpson wrote:

> On 17Jun2011 06:00, Steven D'Aprano
> <steve+comp.lang.python at pearwood.info> wrote: | If we were to have a
> "exit this module early, but without exiting Python | altogether"
> statement, I'd consider "exit" to be the most descriptive | name,
> although it would clash with existing uses of the word, e.g. |
> sys.exit(). Overloading "break" strikes me as disagreeable, but not as |
> disagreeable as overloading "return" or "in" :)
> 
> Just to throw another approach into the mix (because I was thinking
> about the "finally" word), what about:
> 
>   raise StopImport
> 
> along the lines of generators' "raise StopIteration".
> 
> Then the import machinery can catch it, no new keyword is needed and no
> existing keyword needs feature creeping.

The only problem is that the importing module needs to catch it, or else 
you get a traceback. The importer shouldn't need to care what goes in 
inside the module.

Something like this:

spam()
if condition:
    exit  # halt, stop, whatever
ham()
cheese()


should be the equivalent to:


spam()
if not condition:
    ham()
    cheese()


I don't think the use-case for this is convincing enough to need it, but 
it's an interesting concept. I once played around with a mini-language 
for config files that included a "STOP" command, so that:


key = value
STOP
everything under here is ignored


but I think it was a feature in search of a use.



-- 
Steven



More information about the Python-list mailing list