Python中exit、return、sys.exit()等使用實(shí)例和區(qū)別
有這樣一道題目: 字符串標(biāo)識(shí)符.修改例 6-1 的 idcheck.py 腳本,使之可以檢測(cè)長(zhǎng)度為一的標(biāo)識(shí)符,并且可以識(shí)別 Python 關(guān)鍵字,對(duì)后一個(gè)要求,你可以使用 keyword 模塊(特別是 keyword.kelist)來(lái)幫你.
我最初的代碼是:
#!/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
代碼完畢后,我測(cè)試每一條分支,測(cè)試到分支時(shí),必須輸入_d4%等包含非法字符的標(biāo)識(shí)符才能進(jìn)行測(cè)試,我最初以為,sys.exit(0)---正常退出腳本,sys.exit(1)非正常退出腳本,但是實(shí)際情況是/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 <module>
sys.exit(0)
SystemExit: 0
>>>
由此可見(jiàn),這樣做沒(méi)有達(dá)到我預(yù)期如下輸出的效果,那么,問(wèn)題在哪里呢?在于sys.exit()始終會(huì)拋出一個(gè)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:在定義函數(shù)時(shí)從函數(shù)中返回一個(gè)函數(shù)的返回值,終止函數(shù)的執(zhí)行。
exit:下面的代碼中,如果把sys.exit()替換成exit,則exit僅僅跳出離它最近的for循環(huán), print "%s is legal identifier for Python!2" % idInput語(yǔ)句會(huì)被輸出,這里,exit的作用類似于break. 但實(shí)際上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
- python中return不返回值的問(wèn)題解析
- python中return的返回和執(zhí)行實(shí)例
- python 實(shí)現(xiàn)return返回多個(gè)值
- Python Django view 兩種return的實(shí)現(xiàn)方式
- 對(duì)python中return與yield的區(qū)別詳解
- 通過(guò)實(shí)例解析Python return運(yùn)行原理
- python return邏輯判斷表達(dá)式實(shí)現(xiàn)解析
- Python中return語(yǔ)句用法實(shí)例分析
- Python return語(yǔ)句如何實(shí)現(xiàn)結(jié)果返回調(diào)用
相關(guān)文章
Python將圖片批量從png格式轉(zhuǎn)換至WebP格式
最近因?yàn)楣ぷ餍枰パ芯苛讼聀ng的壓縮,發(fā)現(xiàn)轉(zhuǎn)換成webp格式可以小很多,下面給大家分享利用Python將圖片批量從png格式轉(zhuǎn)換至WebP格式的方法,下面來(lái)一起看看。2016-08-08
Python+Django實(shí)現(xiàn)簡(jiǎn)單HelloWord網(wǎng)頁(yè)的示例代碼
本文主要介紹了Python+Django實(shí)現(xiàn)簡(jiǎn)單HelloWord網(wǎng)頁(yè)的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
python實(shí)現(xiàn)簡(jiǎn)單的計(jì)時(shí)器功能函數(shù)
這篇文章主要介紹了python實(shí)現(xiàn)簡(jiǎn)單的計(jì)時(shí)器功能函數(shù),涉及Python操作時(shí)間的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03
Python+騰訊云服務(wù)器實(shí)現(xiàn)每日自動(dòng)健康打卡
本文主要介紹了通過(guò)Python+騰訊云服務(wù)器實(shí)現(xiàn)每日自動(dòng)健康打卡,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-12-12
設(shè)置jupyter中DataFrame的顯示限制方式
這篇文章主要介紹了設(shè)置jupyter中DataFrame的顯示限制方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)2020-04-04
詳解Python實(shí)現(xiàn)同時(shí)支持帶調(diào)用和不調(diào)用帶裝飾器
一般來(lái)說(shuō),不帶參數(shù)裝飾器,再使用時(shí)不加括號(hào),帶參數(shù)的裝飾器使用時(shí)必須加括號(hào),這篇文章主要介紹了Python實(shí)現(xiàn)同時(shí)支持帶調(diào)用和不調(diào)用帶裝飾器的相關(guān)知識(shí),需要的朋友可以參考下2023-06-06
Python logging日志庫(kù)空間不足問(wèn)題解決
這篇文章主要介紹了Python logging日志庫(kù)空間不足問(wèn)題解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09

