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:

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