python assert斷言 的使用

python中assert的使用

寫代碼過程中經常遇到需要調試的時候,而assert就是一種簡單高效的調試方法
比如寫了一個add()函數,但是你不知道寫的對不對,這時候需要對剛寫完的函數進行調試
assert就可以派上用場了

def add(a,b):
	return a+b

def wrongadd(a,b):
	return a-b

def test(fun):
	i = 1
	j = 1
	assert fun(i,j) == 2
	return 'pass!'
	
print(test(add))
print(test(wrongadd))

輸出:pass!
輸出:AssertionError:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章