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

本文翻译自:Python exit commands - why so many and when should each be used?

It seems that python supports many different commands to stop script execution. 似乎python支持许多不同的命令来停止脚本执行。
The choices I've found are: quit() , exit() , sys.exit() , os._exit() 我找到的选择是: quit()exit()sys.exit()os._exit()

Have I missed any? 我错过了吗? What's the difference between them? 它们之间有什么区别? When would you use each? 您什么时候使用?


#1楼

参考:https://stackoom.com/question/1KrBz/Python退出命令-为什么要使用这么多-何时使用


#2楼

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; 当系统和python关闭时; it means less memory is being used after the program is run. 这意味着程序运行后正在使用的内存更少。

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

Otherwise mainly use exit() or quit() . 否则,主要使用exit()quit()


#3楼

The functions *quit() , exit() , and sys.exit() function in the same way: they raise the SystemExit exception. 功能*quit() exit()sys.exit()函数以同样的方式:他们提高SystemExit例外。 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. 因此没有真正的区别,只是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). os._exit()函数很特殊,它不调用任何清理函数就立即退出(例如,它不刷新缓冲区)。 This is designed for highly specialized use cases... basically, only in the child after an os.fork() call. 这是专为高度专业化的用例而设计的...基本上,仅在os.fork()调用之后的子os.fork()

Conclusion 结论

  • Use exit() or quit() in the REPL. 在REPL中使用exit()quit()

  • Use sys.exit() in scripts, or raise SystemExit() if you prefer. 在脚本中使用sys.exit() ,或者根据需要raise SystemExit()

  • Use os._exit() for child processes to exit after a call to os.fork() . 使用os._exit()使子进程在调用os.fork()之后退出。

All of these can be called without arguments, or you can specify the exit status, eg, 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()是可调用的实例对象,但是我认为可以调用它们的函数。


#4楼

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. 包含此功能是为了帮助不了解Python的人。 After all, one of the most likely things a newbie will try to exit Python is typing in quit . 毕竟,新手尝试退出Python的最有可能的事情之一就是输入quit

    Nevertheless, quit should not be used in production code. 不过, quit 应该在产品代码中使用。 This is because it only works if the site module is loaded. 这是因为它仅在加载了site模块时才有效。 Instead, this function should only be used in the interpreter. 相反,此功能应仅在解释器中使用。

  2. exit() is an alias for quit (or vice-versa). exit()quit的别名(反之亦然)。 They exist together simply to make Python more user-friendly. 它们一起存在只是为了使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. 但是,像quit一样, exit在生产代码中被认为是不好用的,应该保留供解释器使用。 This is because it too relies on the site module. 这是因为它也依赖于site模块。

  3. sys.exit() raises the SystemExit exception in the background. sys.exit()在后台引发SystemExit异常。 This means that it is the same as quit and exit in that respect. 这意味着在这方面与quitexit相同。

    Unlike those two however, sys.exit is considered good to use in production code. 但是,与这两者不同, sys.exit被认为可以很好地用于生产代码中。 This is because the sys module will always be there. 这是因为sys模块将始终存在。

  4. os._exit() exits the program without calling cleanup handlers, flushing stdio buffers, etc . os._exit()退出程序, 而无需调用清除处理程序,刷新stdio缓冲区等 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 . 其中最常见的是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: 或者,我认为更好的是,您可以直接在sys.exit执行sys.exit操作并运行:

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. 但是,此选择只是样式上的一个,完全由您决定。


#5楼

sys.exit is the canonical way to exit. sys.exit是退出的规范方法。

Internally sys.exit just raises SystemExit . 在内部sys.exit只会引发SystemExit However, calling sys.exit is more idiomatic than raising SystemExit directly. 但是,调用sys.exit比直接提高SystemExit更惯用。

os.exit is a low-level system call that exits directly without calling any cleanup handlers. os.exit是一个低级系统调用,它直接退出而不调用任何清除处理程序。

quit and exit exist only to provide an easy way out of the Python prompt. quitexit仅是为了提供一种退出Python提示符的简便方法。 This is for new users or users who accidentally entered the Python prompt, and don't want to know the right syntax. 这适用于新用户或不小心输入Python提示符而又不想知道正确语法的用户。 They are likely to try typing exit or quit . 他们可能会尝试键入exitquit 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. 本质上,这只是一种利用,事实是解释程序会打印您在提示符下输入的任何表达式的__repr__

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值