python中exit和break区别_Python中exit()和sys.exit()的区别

^{}是交互式shell的助手-^{}用于程序中。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.

从技术上讲,它们基本上是一样的:提高^{}。sys.exit在sysmodule.c中这样做:static PyObject *

sys_exit(PyObject *self, PyObject *args)

{

PyObject *exit_code = 0;

if (!PyArg_UnpackTuple(args, "exit", 0, 1, &exit_code))

return NULL;

/* Raise SystemExit so callers may catch it or clean up. */

PyErr_SetObject(PyExc_SystemExit, exit_code);

return NULL;

}class Quitter(object):

def __init__(self, name):

self.name = name

def __repr__(self):

return 'Use %s() or %s to exit' % (self.name, eof)

def __call__(self, code=None):

# Shells like IDLE catch the SystemExit, but listen when their

# stdin wrapper is closed.

try:

sys.stdin.close()

except:

pass

raise SystemExit(code)

__builtin__.quit = Quitter('quit')

__builtin__.exit = Quitter('exit')

注意,还有第三个exit选项,即os._exit,它在不调用清理处理程序、刷新stdio缓冲区等的情况下退出(通常只应在fork()之后的子进程中使用)。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值