Python 退出命令 - 为什么有这么多以及何时应该使用每个命令?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19747371/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 14:29:44  来源:igfitidea点击:

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

python

提问by Jonathan

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

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

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

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

回答by Jonathan

Different Means of Exiting

退出方式不同

os._exit():

os._exit()

  • Exit the process without calling the cleanup handlers.
  • 退出进程而不调用清理处理程序。


exit(0):

exit(0)

  • a clean exit without any errors / problems.
  • 干净的退出,没有任何错误/问题。


exit(1):

exit(1)

  • There was some issue / error / problem and that is why the program is exiting.
  • 有一些问题/错误/问题,这就是程序退出的原因。


sys.exit():

sys.exit()

  • When the system and python shuts down; it means less memory is being used after the program is run.
  • 当系统和python关闭时;这意味着程序运行后使用的内存更少。


quit():

quit()

  • Closes the python file.
  • 关闭 python 文件。


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

我认为你没有遗漏任何东西,我建议你习惯quit()exit()

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

如果您使用大文件或使用 python 来控制终端,您将使用sys.exit()andos._exit()主要。

Otherwise mainly use exit()or quit().

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

回答by Dietrich Epp

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

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

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.

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

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

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

  • sys.exit()在脚本中使用,或者raise SystemExit()如果您愿意。

  • 使用os._exit()在通话结束后子进程退出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.

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

Footnotes

脚注

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

*实际上,quit()并且exit()是可调用的实例对象,但我认为将它们称为函数是可以的。

回答by Dietrich Epp

Let me give some information on them:

让我提供一些关于它们的信息:

  1. quit()simply raises the SystemExitexception.

    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, quitshould notbe used in production code. This is because it only works if the sitemodule 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, exitis considered bad to use in production code and should be reserved for use in the interpreter. This is because it too relies on the sitemodule.

  3. sys.exit()also raises the SystemExitexception. This means that it is the same as quitand exitin that respect.

    Unlike those two however, sys.exitis considered good to use in production code. This is because the sysmodule 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.

  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.

    请注意,在给出的四种方法中,只有这一种方法是独一无二的。

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.

综上所述,四种方法都退出程序。然而,前两个被认为在生产代码中使用是不好的,最后一个是非标准的、肮脏的方式,只在特殊场景中使用。所以,如果你想正常退出程序,就用第三种方法:sys.exit



Or, even better in my opinion, you can just do directly what sys.exitdoes behind the scenes and run:

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

raise SystemExit

This way, you do not need to import sysfirst.

这样,您无需先导入sys

However, this choice is simply one on style and is purely up to you.

然而,这个选择只是一种风格,完全取决于你。

回答by oefe

sys.exitis the canonical way to exit.

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

Internally sys.exitjust raises SystemExit. However, calling sys.exitis more idiomatic than raising SystemExitdirectly.

内部sys.exit只是加注SystemExit。然而,跟注sys.exitSystemExit直接加注更惯用。

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

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

quitand exitexist 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 exitor quit. While this will not exit the interpreter, it at least issues a message that tells them a way out:

quit并且exit存在只是为了提供一种摆脱 Python 提示的简单方法。这适用于新用户或不小心进入 Python 提示符且不想知道正确语法的用户。他们可能会尝试输入exitquit。虽然这不会退出解释器,但它至少会发出一条消息,告诉他们一条出路:

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

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