新聞中心
這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
創(chuàng)新互聯(lián)Python教程:python中assert斷言語(yǔ)句怎么用?
Assert斷言語(yǔ)句經(jīng)常是用于確認(rèn)現(xiàn)有的兩個(gè)內(nèi)容是不是滿足條件的,若不滿足,斷言語(yǔ)句引發(fā)異常,大家有沒(méi)有切身體驗(yàn)過(guò)這種異常報(bào)錯(cuò)呢?沒(méi)有的話,下面給大家演示下,正常報(bào)錯(cuò)顯示的現(xiàn)象以及處理辦法,遇到問(wèn)題的小伙伴可以跟著來(lái)了解下啦~

直接用一個(gè)例子演示:
def strToInt10(x):
assert isinstance(x, str), 'x must be string'
assert x.isdigit(), 'x must be digit'
return int(x)
print(strToInt10('10'))
strToInt10([1])
結(jié)果異常:
10 ---- AssertionError Traceback (most recent callin strToInt10(x) 1 def strToInt10(x): ----> 2 assert isinstance(x, str), 'x must be string' 3 assert x.isdigit(), 'x must be digit' 4 return int(x) 5 print(strToInt10('10')) AssertionError: x must be string
雖然上報(bào)代碼顯示報(bào)錯(cuò),但是我們可以使用try處理這些異常的,大家如果需要調(diào)用這個(gè)語(yǔ)句,可以依照上述小編給大家演示的效果進(jìn)行實(shí)踐哦~
網(wǎng)站欄目:創(chuàng)新互聯(lián)Python教程:python中assert斷言語(yǔ)句怎么用?
網(wǎng)頁(yè)URL:http://fisionsoft.com.cn/article/dhdjcpj.html


咨詢
建站咨詢
