Python exit commands - why so many and when should each be used?

后端 未结 4 1339
悲哀的现实
悲哀的现实 2020-11-22 10:04

It seems that python supports many different commands to stop script execution.
The choices I\'ve found are: quit(), exit(), sys.exit()

4条回答
  •  失恋的感觉
    2020-11-22 10:38

    Different Means of Exiting

    os._exit():

    • Exit the process without calling the cleanup handlers.

    exit(0):

    • a clean exit without any errors / problems.

    exit(1):

    • There was some issue / error / problem and that is why the program is exiting.

    sys.exit():

    • When the system and python shuts down; it means less memory is being used after the program is run.

    quit():

    • Closes the python file.

    Summary

    Basically they all do the same thing, however, it also depends on what you are doing it for.

    I don't think you left anything out and I would recommend getting used to quit() or exit().

    You would use sys.exit() and os._exit() mainly if you are using big files or are using python to control terminal.

    Otherwise mainly use exit() or quit().

提交回复
热议问题