這篇文章主要介紹了Python中exit、return、sys.exit()等使用實例和區別,本文是一個實際項目中的總結,需要的朋友可以參考下
有這樣一道題目: 字符串標識符.修改例 6-1 的 idcheck.py 腳本,使之可以檢測長度為一的標識符,並且可以識別 Python 關鍵字,對後一個要求,你可以使用 keyword 模塊(特別是 keyword.kelist)來幫你.
我最初的代碼是:
代碼如下:
#!/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