The site module (which is imported automatically during startup, except if the -S command-line option is given) adds several constants to the built-in namespace (e.g. exit). They are useful for the interactive interpreter shell and should not be used in programs.
如果我在代码中使用
exit()
并在shell中运行它,它将显示一条消息,询问是否要终止程序。真令人不安。 See here但在这种情况下
sys.exit()
更好。它关闭程序,不创建任何对话框。^{} 是交互式shell的助手-^{} 用于程序中。
从技术上讲,它们基本上是一样的:提高^{} 。
sys.exit
在sysmodule.c中这样做:而
exit
分别在site.py和_sitebuiltins.py中定义。注意,还有第三个exit选项,即os._exit,它在不调用清理处理程序、刷新stdio缓冲区等的情况下退出(通常只应在
fork()
之后的子进程中使用)。相关问题 更多 >
编程相关推荐