Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between exit() and sys.exit() in Python

Tags:

python

exit

In Python, there are two similarly-named functions, exit() and sys.exit(). What's the difference and when should I use one over the other?

like image 819
Drake Guan Avatar asked Jun 28 '11 03:06

Drake Guan


People also ask

What is the difference between exit () and SYS exit ()?

exit is a helper for the interactive shell - sys. exit is intended for use in programs. The site module (which is imported automatically during startup, except if the -S command-line option is given) adds several constants to the built-in namespace (e.g. exit ).

What is the use of sys exit () in Python?

exit() function allows the developer to exit from Python. The exit function takes an optional argument, typically an integer, that gives an exit status. Zero is considered a “successful termination”.

What is the difference between break and exit () in Python?

A "break" is only allowed in a loop (while or for), and it causes the loop to end but the rest of the program continues. On the other hand "sys. exit()" aborts the execution of the current program and passes control to the environment.

When should you use SYS exit?

Whenever we want to exit from the program without reaching the end of the program, we can use the different exit programs available in python. The most standard and commonly used one is the sys. exit() function. We can also usu raise SystemExit to exit out of the program.


2 Answers

exit is a helper for the interactive shell - sys.exit is intended for use in programs.

The site module (which is imported automatically during startup, except if the -S command-line option is given) adds several constants to the built-in namespace (e.g. exit). They are useful for the interactive interpreter shell and should not be used in programs.


Technically, they do mostly the same: raising SystemExit. sys.exit does so in sysmodule.c:

static PyObject * sys_exit(PyObject *self, PyObject *args) {     PyObject *exit_code = 0;     if (!PyArg_UnpackTuple(args, "exit", 0, 1, &exit_code))         return NULL;     /* Raise SystemExit so callers may catch it or clean up. */     PyErr_SetObject(PyExc_SystemExit, exit_code);    return NULL; } 

While exit is defined in site.py and _sitebuiltins.py, respectively.

class Quitter(object):     def __init__(self, name):         self.name = name     def __repr__(self):         return 'Use %s() or %s to exit' % (self.name, eof)     def __call__(self, code=None):         # Shells like IDLE catch the SystemExit, but listen when their         # stdin wrapper is closed.         try:             sys.stdin.close()         except:             pass         raise SystemExit(code) __builtin__.quit = Quitter('quit') __builtin__.exit = Quitter('exit') 

Note that there is a third exit option, namely os._exit, which exits without calling cleanup handlers, flushing stdio buffers, etc. (and which should normally only be used in the child process after a fork()).

like image 55
miku Avatar answered Oct 07 '22 12:10

miku


If I use exit() in a code and run it in the shell, it shows a message asking whether I want to kill the program or not. It's really disturbing. See here

But sys.exit() is better in this case. It closes the program and doesn't create any dialogue box.

like image 43
Ramisa Anjum Aditi Avatar answered Oct 07 '22 13:10

Ramisa Anjum Aditi