Let me give some information on them:
quit()
simply raises theSystemExit
exception.Furthermore, if you print it, it will give a message:>>> print (quit) Use quit() or Ctrl-Z plus Return to exit >>>
This functionality was included to help people who do not know Python. After all, one of the most likely things a newbie will try to exit Python is typing inquit
.Nevertheless,quit
should not be used in production code. This is because it only works if thesite
module is loaded. Instead, this function should only be used in the interpreter.exit()
is an alias forquit
(or vice-versa). They exist together simply to make Python more user-friendly.Furthermore, it too gives a message when printed:>>> print (exit) Use exit() or Ctrl-Z plus Return to exit >>>
However, likequit
,exit
is considered bad to use in production code and should be reserved for use in the interpreter. This is because it too relies on thesite
module.sys.exit()
also raises theSystemExit
exception. This means that it is the same asquit
andexit
in that respect.Unlike those two however,sys.exit
is considered good to use in production code. This is because thesys
module will always be there.os._exit()
exits the program without calling cleanup handlers, flushing stdio buffers, etc. Thus, it is not a standard way to exit and should only be used in special cases. The most common of these is in the child process(es) created byos.fork
.Note that, of the four methods given, only this one is unique in what it does.
Summed up, all four methods exit the program. However, the first two are considered bad to use in production code and the last is a non-standard, dirty way that is only used in special scenarios. So, if you want to exit a program normally, go with the third method: sys.exit
.
Or, even better in my opinion, you can just do directly what sys.exit
does behind the scenes and run:
raise SystemExit
This way, you do not need to import sys
first.
However, this choice is simply one on style and is purely up to you.