Python退出命令-为什么要使用这么多?何时使用?

问题:Python退出命令-为什么要使用这么多?何时使用?

似乎python支持许多不同的命令来停止脚本执行。
我发现的选择是: quit()exit()sys.exit()os._exit()

我错过了吗?它们之间有什么区别?您什么时候使用?

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

Have I missed any? What’s the difference between them? When would you use each?


回答 0

让我给他们一些信息:

  1. quit()只是引发SystemExit异常。

    此外,如果您打印它,它将显示一条消息:

    >>> print (quit)
    Use quit() or Ctrl-Z plus Return to exit
    >>>
    

    包含此功能是为了帮助不了解Python的人。毕竟,新手尝试退出Python的最有可能的事情之一就是输入quit

    然而,quit应该不是在生产代码中使用。这是因为它仅在site模块加载后才起作用。相反,此功能应仅在解释器中使用。

  2. exit()是的别名quit(反之亦然)。它们一起存在只是为了使Python更加用户友好。

    此外,在打印时它还会给出一条消息:

    >>> print (exit)
    Use exit() or Ctrl-Z plus Return to exit
    >>>
    

    然而,像quitexit被认为是不好的产品代码使用,并应保留在解释使用。这是因为它也依赖于site模块。

  3. sys.exit()也引发了SystemExit异常。这意味着,它是相同的quit,并exit在这方面。

    但是,与这两者不同的是,sys.exit在生产代码中很好地使用它。这是因为sys模块将始终存在。

  4. os._exit()退出程序而不调用清理处理程序,刷新stdio缓冲区等。因此,这不是退出的标准方法,仅应在特殊情况下使用。其中最常见的是在所创建的子进程中os.fork

    请注意,在给出的四种方法中,只有一种是唯一的。

总结起来,所有四种方法都退出程序。但是,前两个在生产代码中被认为不好用,最后一个是非标准的,肮脏的方式,仅在特殊情况下使用。因此,如果要正常退出程序,请使用第三个方法:sys.exit


或者,我认为更好的是,您可以直接sys.exit执行幕后操作并运行:

raise SystemExit

这样,您无需先导入sys

但是,此选择只是样式上的一个,完全由您决定。

Let me give some information on them:

  1. quit() simply raises the SystemExit 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 in quit.

    Nevertheless, quit should not be used in production code. This is because it only works if the site module is loaded. Instead, this function should only be used in the interpreter.

  2. exit() is an alias for quit (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, like quit, 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 the site module.

  3. sys.exit() also raises the SystemExit exception. This means that it is the same as quit and exit in that respect.

    Unlike those two however, sys.exit is considered good to use in production code. This is because the sys module will always be there.

  4. 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 by os.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.


回答 1

函数*quit()exit()sys.exit()以相同的方式起作用:它们引发SystemExit异常。因此,有没有真正的区别,不同之处在于sys.exit()始终可用,但exit()quit()是唯一可用的,如果site模块是进口的。

os._exit()函数很特殊,它不调用任何清理函数就立即退出(例如,它不刷新缓冲区)。这是针对高度专业化的用例而设计的……基本上,仅在os.fork()通话后的孩子中。

结论

  • 在REPL中使用exit()quit()

  • sys.exit()在脚本中使用,或者raise SystemExit()根据需要使用。

  • 使用os._exit()在通话结束后子进程退出os.fork()

所有这些都可以在不带参数的情况下调用,或者您可以指定退出状态,例如,exit(1)raise SystemExit(1)以状态1退出。请注意,可移植程序仅限于退出状态代码,范围为0-255,如果raise SystemExit(256)在许多系统上,这将被截断,您的进程实际上将以状态0退出。

脚注

*其实,quit()并且exit()是可调用的实例对象,但我认为这没关系给他们打电话的功能。

The functions*quit(), exit(), and sys.exit() function in the same way: they raise the SystemExit exception. So there is no real difference, except that sys.exit() is always available but exit() and quit() are only available if the site module is imported.

The os._exit() function is special, it exits immediately without calling any cleanup functions (it doesn’t flush buffers, for example). This is designed for highly specialized use cases… basically, only in the child after an os.fork() call.

Conclusion

  • Use exit() or quit() in the REPL.

  • Use sys.exit() in scripts, or raise SystemExit() if you prefer.

  • Use os._exit() for child processes to exit after a call to os.fork().

All of these can be called without arguments, or you can specify the exit status, e.g., exit(1) or raise SystemExit(1) to exit with status 1. Note that portable programs are limited to exit status codes in the range 0-255, if you raise SystemExit(256) on many systems this will get truncated and your process will actually exit with status 0.

Footnotes

* Actually, quit() and exit() are callable instance objects, but I think it’s okay to call them functions.


回答 2

退出的不同方式

os._exit()

  • 退出过程而不调用清理处理程序。

exit(0)

  • 干净的出口,没有任何错误/问题。

exit(1)

  • 存在一些问题/错误/问题,这就是程序退出的原因。

sys.exit()

  • 当系统和python关闭时;这意味着程序运行后正在使用的内存更少。

quit()

  • 关闭python文件。

摘要

基本上他们都做相同的事情,但是,这还取决于您要做什么。

我认为您不会遗漏任何东西,建议您习惯quit()exit()

您将使用sys.exit()并且os._exit()主要是在使用大文件或使用python控制终端的情况下。

否则主要使用exit()quit()

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


回答 3

sys.exit 是退出的规范方法。

内部sys.exit只是提高SystemExit。但是,呼叫sys.exitSystemExit直接发起更为惯用。

os.exit 是一个低级系统调用,它直接退出而不调用任何清除处理程序。

quit并且exit仅用于提供一种简便的方法来退出Python提示符。这适用于新用户或不小心输入Python提示符而又不想知道正确语法的用户。他们可能会尝试输入exitquit。尽管这不会退出解释器,但它至少会发出一条消息,告诉他们出路:

>>> exit
Use exit() or Ctrl-D (i.e. EOF) to exit
>>> exit()
$

本质上,这只是一种利用,事实是解释器会打印__repr__您在提示符下输入的任何表达式的事实。

sys.exit is the canonical way to exit.

Internally sys.exit just raises SystemExit. However, calling sys.exitis more idiomatic than raising SystemExit directly.

os.exit is a low-level system call that exits directly without calling any cleanup handlers.

quit and exit exist only to provide an easy way out of the Python prompt. This is for new users or users who accidentally entered the Python prompt, and don’t want to know the right syntax. They are likely to try typing exit or quit. While this will not exit the interpreter, it at least issues a message that tells them a way out:

>>> exit
Use exit() or Ctrl-D (i.e. EOF) to exit
>>> exit()
$

This is essentially just a hack that utilizes the fact that the interpreter prints the __repr__ of any expression that you enter at the prompt.