我寫了這段代碼:def func(collection_type):
assert isinstance(collection_type,(list,set))然后我寫道:func(collection_type=set)我得到了斷言錯誤
1 回答

拉莫斯之舞
TA貢獻1820條經驗 獲得超10個贊
assert isinstance(collection_type,(list,set))
。這將測試給定的實例是否屬于(list,set)
。所以嘗試使用列表或設置的實例,如下所示,
func(collection_type=[1, 2, 3])
或func(collection_type={1, 2, 3})
。
在Python中,所有類都是type
類的實例。所以,如果你通過了課程本身,就像func(collection_type=set)
. 它將檢查type
,因為只有(list,set)
那里存在,所以它會引發(fā)斷言錯誤。
如果你想用空集進行測試,請嘗試func(collection_type=set())
添加回答
舉報
0/150
提交
取消