Python中exit()和sys.exit()之间的区别

问题:Python中exit()和sys.exit()之间的区别

在Python中,有两个类似的函数,exit()sys.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?


回答 0

exit是交互式外壳的帮助sys.exit程序- 旨在在程序中使用。

site模块(启动时会自动导入,除非指定了-S命令行选项)会向内置命名空间(例如exit添加多个常量。它们对于交互式解释程序外壳很有用,不应在程序中使用


从技术上讲,它们的作用大致相同:提高SystemExitsys.exitsysmodule.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;
}

虽然分别exitsite.py_sitebuiltins.py中定义。

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')

请注意,还有第三个退出选项os._exit,它退出时不调用清除处理程序,刷新stdio缓冲区等(并且通常仅应在之后的子进程中使用fork())。

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()).


回答 1

如果我exit()在代码中使用并在外壳中运行它,则会显示一条消息,询问我是否要终止该程序。真是令人不安。 看这里

但是sys.exit()在这种情况下更好。它关闭程序,并且不创建任何对话框。

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.