Python中exit、return、sys.exit()等使用实例和区别
我最初的代码是:
#!/usr/bin/env python
import string
import keyword
import sys
#Get all keyword for python
#keyword.kwlist
#['and', 'as', 'assert', 'break', ...]
keyWords = keyword.kwlist
#Get all character for identifier
#string.letters ==> 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
#string.digits ==> '0123456789'
charForId = string.letters + "_"
numForId = string.digits
idInput = raw_input("Input your words,please!")
if idInput in keyWords:
print "%s is keyword fot Python!" % idInput
else:
lenNum = len(idInput)
if(1 == lenNum):
if(idInput in charForId and idInput != "_"):
print "%s is legal identifier for Python!" % idInput
else:
#It's just "_"
print "%s isn't legal identifier for Python!" % idInput
else:
if(idInput[0:1] in charForId):
legalstring = charForId + numForId
for item in idInput[1:]:
if (item not in legalstring):
print "%s isn't legal identifier for Python!" % idInput
sys.exit(0)
print "%s is legal identifier for Python!2" % idInput
else:
print "%s isn't legal identifier for Python!3" % idInput
代码完毕后,我测试每一条分支,测试到分支时,必须输入_d4%等包含非法字符的标识符才能进行测试,我最初以为,sys.exit(0)---正常退出脚本,sys.exit(1)非正常退出脚本,但是实际情况是/9sys.exit(1),仅输出返回码不同):
if (item not in legalstring):
print "%s isn't legal identifier for Python!" % idInput
sys.exit(0)
Input your words,please!_d4%
_d4% isn't legal identifier for Python!
Traceback (most recent call last):
File "E:/python/idcheck.py", line 37, in
sys.exit(0)
SystemExit: 0
>>>
由此可见,这样做没有达到我预期如下输出的效果,那么,问题在哪里呢?在于sys.exit()始终会抛出一个SystemExit异常。
Input your words,please!_d4%
_d4% isn't legal identifier for Python!
#!/usr/bin/env python
import string
import keyword
import sys
import traceback
try:
#Get all keyword for python
#keyword.kwlist
#['and', 'as', 'assert', 'break', ...]
keyWords = keyword.kwlist
#Get all character for identifier
#string.letters ==> 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
#string.digits ==> '0123456789'
charForId = string.letters + "_"
numForId = string.digits
idInput = raw_input("Input your words,please!")
if idInput in keyWords:
print "%s is keyword fot Python!" % idInput
else:
lenNum = len(idInput)
if(1 == lenNum):
if(idInput in charForId and idInput != "_"):
print "%s is legal identifier for Python!" % idInput
else:
#It's just "_"
print "%s isn't legal identifier for Python!" % idInput
else:
if(idInput[0:1] in charForId):
legalstring = charForId + numForId
for item in idInput[1:]:
if (item not in legalstring):
print "%s isn't legal identifier for Python!" % idInput
sys.exit()
print "%s is legal identifier for Python!2" % idInput
else:
print "%s isn't legal identifier for Python!3" % idInput
except SystemExit:
pass
except:
traceback.print_exc()
上面的代码获取sys.exit()抛出的SystemExit异常。
return:在定义函数时从函数中返回一个函数的返回值,终止函数的执行。
exit:下面的代码中,如果把sys.exit()替换成exit,则exit仅仅跳出离它最近的for循环, print "%s is legal identifier for Python!2" % idInput语句会被输出,这里,exit的作用类似于break. 但实际上break和exit作用并不同
for item in idInput[1:]:
if (item not in legalstring):
print "%s isn't legal identifier for Python!" % idInput
sys.exit()
print "%s is legal identifier for Python!2" % idInput
声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
相关文章
相关视频
专题推荐
-
独孤九贱-php全栈开发教程
全栈 170W+
主讲:Peter-Zhu 轻松幽默、简短易学,非常适合PHP学习入门
-
玉女心经-web前端开发教程
入门 80W+
主讲:灭绝师太 由浅入深、明快简洁,非常适合前端学习入门
-
天龙八部-实战开发教程
实战 120W+
主讲:西门大官人 思路清晰、严谨规范,适合有一定web编程基础学习
上一篇: Node.js 数据加密传输浅析
下一篇: mac php没有GD库怎么办
推荐阅读
-
Python中exit、return、sys.exit()等使用实例和区别
-
Python中exit、return、sys.exit()等使用实例和区别
-
Python中晦涩难懂的概念,定义类创建和使用实例,单下划线和双下划线,__init__构造函数等
-
Python中exit、return、sys.exit()等使用实例和区别
-
Python中exit、return、sys.exit()等使用实例和区别
-
PHP中跳出多重循环使用break,continue,goto,return,exit的用法和区别
-
PHP中跳出多重循环使用break,continue,goto,return,exit的用法和区别
网友评论
文明上网理性发言,请遵守 新闻评论服务协议
我要评论