[英]I want to return KeyError not None
我想返回KeyError
但是它返回None
。 我不理解為什么。
if index == self.length or self.slots[index]==None:
raise KeyError
您不會引發 KeyError
因為您沒有輸入if
-branch。 而且您不會輸入if
-branch,因為您的條件都不true
。
如果測試用例(未顯示!)確實引發KeyError
,則可能需要調整條件。
要調試此問題,您可以插入一些print
語句:
print(index, self.length, index==self.length)
print(self.slots[index], self.slots[index]==None)
if index == self.length or self.slots[index]==None:
raise KeyError
或使用您選擇的調試器。
但是一些評論:
>=
==
長度(包括==
!)。 is
與None
進行is
。 None
和NotImplemented
是有保證的單例。 KeyError
使用異常消息,例如, raise KeyError('index out of bounds or item is None! Try again.')
return
的函數將返回None
。 這可能就是為什么您的代碼返回None
原因!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.