Reputation: 165
In several Python scripts included with Anaconda, sys.exit() is applied to a main() function. Here's an example from a script to launch jupyter notebook :
import re
import sys
from jupyter_core.command import main
if __name__ == '__main__':
sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
sys.exit(main())
What's the purpose of using sys.exit() as a way to wrap the call of the function main() ? According to the Python documentation sys.exit() just raises a SystemExit exception signaling an intention to exit the interpreter (https://docs.python.org/3/library/sys.html#sys.exit).
Thanks.
Upvotes: 2
Views: 1089
Reputation: 20590
Read the next part of the documentation that you have linked:
sys.exit([arg])
Raise a SystemExit exception, signaling an intention to exit the interpreter.
The optional argument arg can be an integer giving the exit status.
So the argument is used as an exit code for the current process.
In the cases you mention where we have sys.exit(main())
, the main()
function itself returns an integer depending on how it ended which is then passed through as an exit code for sys.exit
Upvotes: 3