Python退出命令——为什么要使用这么多命令?

时间:2021-06-24 17:02:41

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()、sy .exit()、os._exit()

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

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

4 个解决方案

#1


479  

Let me give some information on them:

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

  1. quit raises the SystemExit exception behind the scenes.

    quit在幕后引发了SystemExit异常。

    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.

    这个功能是用来帮助不了解Python的人的。毕竟,新手最可能尝试退出Python的事情之一就是输入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.

    然而,在生产代码中不应该使用quit。这是因为它只有在加载站点模块时才有效。相反,这个函数应该只在解释器中使用。

  2. exit is an alias for quit (or vice-versa). They exist together simply to make Python more user-friendly.

    exit是quit(退出)的别名(反之亦然)。它们共存只是为了使Python更加用户友好。

    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.

    但是,像quit一样,退出在生产代码中被认为是不好的,应该保留在解释器中使用。这是因为它也依赖于站点模块。

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

    sys。退出会在后台引发SystemExit异常。这意味着在这方面,退出和退出是一样的。

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

    然而,与这两家公司不同的是,sys。在产品代码中使用exit被认为是很好的。这是因为sys模块将始终存在。

  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.

    操作系统。_exit退出程序,不调用清理处理程序、刷新stdio缓冲区等。因此,它不是退出的标准方法,只能在特殊情况下使用。最常见的是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.

总之,四个方法都退出了程序。但是,前两种方法在生产代码中被认为是不好的,最后一种方法是非标准的、肮脏的,只在特殊场景中使用。因此,如果您想要正常地退出程序,请使用第三种方法: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.

这样,您不需要先导入sys。

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

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

#2


46  

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.

函数* quit()、exit()和sys.exit()函数采用相同的方法:它们提高SystemExit异常。因此没有真正的区别,除了sy .exit()始终可用,但是exit()和quit()只有在导入站点模块时才可用。

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()函数是特殊的,它立即退出而不调用任何清理函数(例如,它不刷新缓冲区)。这是为高度专门化的用例设计的……基本上,只有在孩子之后才会调用。fork()调用。

Conclusion

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

    在REPL中使用exit()或quit()。

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

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

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

    在调用os.fork()之后,对子进程使用os._exit()。

Footnotes

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

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

#3


15  

sys.exit is the canonical way to exit.

sys。退出是退出的标准方法。

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

内部系统。只是提出了SystemExit退出。然而,调用系统。存在比直接提高系统出口更有习语性。

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

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

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:

退出和退出的存在只是为了提供一种简单的方法来摆脱Python提示符。这是针对不小心输入Python提示符的新用户或用户,也不希望知道正确的语法。他们可能会尝试退出或退出。虽然这将不会退出解释器,但它至少会发出一条消息,告诉他们一条出路:

>>> 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__的技巧。

#4


12  

Different Means of Exiting

os._exit():

os._exit():

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

exit(0):

退出(0):

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

exit(1):

退出(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():

辞职():

  • 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来控制终端,那么您将主要使用system .exit()和os._exit()。

Otherwise mainly use exit() or quit().

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

#1


479  

Let me give some information on them:

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

  1. quit raises the SystemExit exception behind the scenes.

    quit在幕后引发了SystemExit异常。

    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.

    这个功能是用来帮助不了解Python的人的。毕竟,新手最可能尝试退出Python的事情之一就是输入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.

    然而,在生产代码中不应该使用quit。这是因为它只有在加载站点模块时才有效。相反,这个函数应该只在解释器中使用。

  2. exit is an alias for quit (or vice-versa). They exist together simply to make Python more user-friendly.

    exit是quit(退出)的别名(反之亦然)。它们共存只是为了使Python更加用户友好。

    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.

    但是,像quit一样,退出在生产代码中被认为是不好的,应该保留在解释器中使用。这是因为它也依赖于站点模块。

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

    sys。退出会在后台引发SystemExit异常。这意味着在这方面,退出和退出是一样的。

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

    然而,与这两家公司不同的是,sys。在产品代码中使用exit被认为是很好的。这是因为sys模块将始终存在。

  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.

    操作系统。_exit退出程序,不调用清理处理程序、刷新stdio缓冲区等。因此,它不是退出的标准方法,只能在特殊情况下使用。最常见的是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.

总之,四个方法都退出了程序。但是,前两种方法在生产代码中被认为是不好的,最后一种方法是非标准的、肮脏的,只在特殊场景中使用。因此,如果您想要正常地退出程序,请使用第三种方法: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.

这样,您不需要先导入sys。

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

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

#2


46  

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.

函数* quit()、exit()和sys.exit()函数采用相同的方法:它们提高SystemExit异常。因此没有真正的区别,除了sy .exit()始终可用,但是exit()和quit()只有在导入站点模块时才可用。

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()函数是特殊的,它立即退出而不调用任何清理函数(例如,它不刷新缓冲区)。这是为高度专门化的用例设计的……基本上,只有在孩子之后才会调用。fork()调用。

Conclusion

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

    在REPL中使用exit()或quit()。

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

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

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

    在调用os.fork()之后,对子进程使用os._exit()。

Footnotes

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

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

#3


15  

sys.exit is the canonical way to exit.

sys。退出是退出的标准方法。

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

内部系统。只是提出了SystemExit退出。然而,调用系统。存在比直接提高系统出口更有习语性。

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

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

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:

退出和退出的存在只是为了提供一种简单的方法来摆脱Python提示符。这是针对不小心输入Python提示符的新用户或用户,也不希望知道正确的语法。他们可能会尝试退出或退出。虽然这将不会退出解释器,但它至少会发出一条消息,告诉他们一条出路:

>>> 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__的技巧。

#4


12  

Different Means of Exiting

os._exit():

os._exit():

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

exit(0):

退出(0):

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

exit(1):

退出(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():

辞职():

  • 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来控制终端,那么您将主要使用system .exit()和os._exit()。

Otherwise mainly use exit() or quit().

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