python | exit
2016-09-13 22:46
183 查看
exit
@(python)[语法]exit() and sys.exit()
exit is a helper for the interactive shell - sys.exit is intended for use in programs.交互下用 exit(), 程序中用 sys.exit()
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.
Technically, they do mostly the same:
raising SystemExit.
sys.exit does so in 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; }
While exit is defined in site.py:
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')
os._exit
Note that there is a third exit option, namelyos._exit, which exits without calling cleanup handlers, flushing stdio buffers, etc. (and which should normally only be used in the child process after a fork()).
相关文章推荐
- Python 运行出现Process finished with exit code -1073741515 (0xC0000135)错误
- 【ZZ】windows+python2.7在IDLE中执行sys.exit()出现的问题及解决方案
- 关于pip install安装python依赖包时报错:command 'gcc' failed with exit status 1的解决方案
- python的两种退出方式 os._exit() vs sys.exit()
- 安装MySQL-python-1.2.3c1出现“error: command 'gcc' failed with exit status 1”错误
- python基础之 Python os._exit() sys.exit() exit()区别
- python unit test to assert a method calls sys.exit()
- mac下安装mysql-python包报unable to execute llvm-gcc-4.2: No such file or directory error: command 'llvm-gcc-4.2' failed with exit status 1解决方法
- MAC安装python模块碰到“error: command 'cc' failed with exit status 1”解决办法
- python 的exit,return,sys.exit()等的区别与使用方法
- python psutil 编译中断。 error: command 'gcc' failed with exit status 1
- python 之 os._exit() sys.exit() 、exit()
- python基础----实现上下文管理协议__enter__和__exit__
- python sys.exit()、break、 exit()、quit()、os._exit()、return的区别
- Python os._exit() sys.exit() exit()区别
- MySQL-python “error: command 'gcc' failed with exit status 1”错误
- 解决Python扩展程序编译安装的error: command 'mt.exe' failed with exit status 31
- windows+python2.7在IDLE中执行sys.exit()出现的问题及解决方案
- Python特性(十五): context manager的__exit__方法对异常的处理
- Python os._exit, sys.exit