python退出程序-Python退出命令的总结

Python exit command

@(Python入门)

[TOC]

Which

exit()

quit()

Ctrl+Z Enter

sys.exit()

raise SystemExit

os.exit

How and When

quit

quit raises the SystemExit exception behind the scenes.

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. After all, one of the most likely things a newbie will try to exit Python is typing in quit.

Nevertheless, quit should not be used in production code. This is because it only works if the site module is loaded. Instead, this function should only be used in the interpreter.

exit

exit is an alias for quit (or vice-versa). They exist together simply to make Python more user-friendly.

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. This is because it too relies on the site module.

sys.exit

sys.exit raises the SystemExit exception in the background. This means that it is the same as quit and exit in that respect.

Unlike those two however, sys.exit is considered good to use in production code. This is because the sys module will always be there.

os._exit

os._exit exits the program without calling cleanup handlers, flushing stdio buffers, etc. 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.

Ctrl+Z

Ctrl+Z is a qucik-operation of exit and quit, which means Ctrl+Z is the same with them.

Suggestion

use

raise SystemExit

to exit, so u don't need to import sys first.

附录

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. They are useful for the interactive interpreter shell and should not be used in programs.

quit([code=None])

exit([code=None])

Objects that when printed, print a message like “Use quit() or Ctrl-D (i.e. EOF, end of file) to exit”, and when called, raise SystemExit with the specified exit code.

Actually, I don't understand the last sentence. Because when I use quit and exit in Python(win10,x64,3.6),it only shows 'Use quit() or Ctrl-Z plus Return to exit'

db0a98c4427e

Figure.1

So, I wonder if there is a problem or bug in there or just my English is awful.

By the way, I have tried to use Ctrl-D to drop out of Python, but failed. Then I found an interesting vent about Ctrl-D.

sys.exit([arg])

Exit from Python. This is implemented by raising the SystemExit exception, so cleanup actions specified by finally clauses of try statements are honored, and it is possible to intercept the exit attempt at an outer level.

what is the meaning of 'raising exception'?

https://docs.python.org/3.4/tutorial/errors.html#exceptions

This may mean that quit, exit, and sys.exit are one kind of SystemExit, but the three would be used in different circumstance.

The optional argument arg can be an integer giving the exit status (defaulting to zero), or another type of object. If it is an integer, zero is considered “successful termination” and any nonzero value is considered “abnormal termination(结局)” by shells and the like. Most systems require it to be in the range 0–127, and produce undefined results otherwise. Some systems have a convention for assigning specific meanings to specific exit codes, but these are generally underdeveloped; Unix programs generally use 2 for command line syntax errors and 1 for all other kind of errors. If another type of object is passed, None is equivalent to passing zero, and any other object is printed to stderr and results in an exit code of 1. In particular, sys.exit("some error message") is a quick way to exit a program when an error occurs.

Since exit() ultimately “only” raises an exception, it will only exit the process when called from the main thread, and the exception is not intercepted.

Changed in version 3.6: If an error occurs in the cleanup after the Python interpreter has caught SystemExit (such as an error flushing buffered data in the standard streams), the exit status is changed to 120.

Reference

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值