Python似乎支持很多不同的命令来停止脚本执行.
我找到的选项有:quit()
、exit()
、sys.exit()
、os._exit()
我错过什么了吗?
Python似乎支持很多不同的命令来停止脚本执行.
我找到的选项有:quit()
、exit()
、sys.exit()
、os._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.
os._exit()
函数是特殊的,它会立即退出,而不调用任何清理函数(例如,它不会刷新缓冲区).这是为高度专业化的用例设计的...基本上,只有在打了os.fork()
次电话之后,子元素才会有这种感觉.
在REPL中使用exit()
或quit()
.
在脚本中使用sys.exit()
,如果愿意,可以使用raise SystemExit()
.
调用os.fork()
后,子进程退出时使用os._exit()
.
所有这些都可以在没有参数的情况下调用,也可以指定退出状态,例如,exit(1)
或raise SystemExit(1)
以状态1退出.请注意,可移植程序的退出状态代码限制在0-255范围内,如果在许多系统上执行raise SystemExit(256)
,这将被截断,并且进程将实际以0状态退出.
实际上,quit()
和exit()
是可调用的实例对象,但我认为可以调用它们的函数.