[Python-ideas] breaking out of module execution

Cameron Simpson cs at zip.com.au
Thu Apr 26 01:32:10 CEST 2012

On 24Apr2012 13:23, Eric Snow <ericsnowcurrently at gmail.com> wrote:
| In a function you can use a return statement to break out of execution
| in the middle of the function.  With modules you have no recourse.
| This is akin to return statements being allowed only at the end of a
| function.

Something very similar came up some months ago I think.

My personal suggestion was:

  raise StopImport

to cleanly exit a module without causing an ImportError, just as exiting
a generator raises Stopiteration in the caller. In this case the caller
is the import machinery, which should consider this except a clean
completion of the import.

Cameron Simpson <cs at zip.com.au> DoD#743

Disclaimer: Opinions expressed here are CORRECT, mine, and not PSLs or NMSUs..
        - Larry Cunningham <larry at psl.nmsu.edu>

More information about the Python-ideas mailing list