[Python-ideas] breaking out of module execution

Antoine Pitrou solipsis at pitrou.net
Tue Apr 24 21:40:58 CEST 2012


On Tue, 24 Apr 2012 13:23:53 -0600
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.
> 
> There are a small number of ways you can work around this, but they
> aren't great.  This includes using wrapper modules or import hooks or
> sometimes from-import-*.  Otherwise, if your module's execution is
> conditional, you end up indenting everything inside an if/else
> statement.

I think good practice should lead you to put your initialization code
in a dedicated function that you call from your module toplevel. In
this case, breaking out of execution is a matter of adding a return
statement.

I'm not sure the particular use cases you brought up are a good enough
reason to add a syntactical construct.

Regards

Antoine.





More information about the Python-ideas mailing list