[issue39452] Improve the __main__ module documentation

Géry report at bugs.python.org
Tue Aug 31 18:55:18 EDT 2021


Géry <gery.ogam at gmail.com> added the comment:

No worries, it was almost twenty years ago.

> But it's probably better to raise a dedicated exception in that case and catch it in main(), rather than just calling sys.exit() deep inside the other code.

Yes I agree, and I think you explained very clearly why it is better in the blog post:

> Another refinement is to define a Usage() exception, which we catch in an except clause at the end of main():
> […]
> This gives the main() function a single exit point, which is preferable over multiple return 2 statements.

So I think you made two independent points:

- raising a dedicated exception instead of calling `sys.exit` inside nested functions and catching it inside `main` allows a single exit point;
- calling `sys.exit` outside of `main` instead of inside prevents exiting the Python interpreter in an interactive session.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue39452>
_______________________________________


More information about the Python-bugs-list mailing list