我在這個函數中一直有一個錯誤,我找不到原因。我以爲我第二次修復它,但沒有看到爲什麼這仍然是任何問題。該錯誤一次又一次彈出。Python - 關鍵錯誤的防禦性編程
這是我的第一個錯誤,但我修復它。
File "running.py", line 332, in run
TypeError: argument of type 'NoneType' is not iterable ================================================================================
05Oct 03:06:48: Exit status: 1
這是我當前的代碼
def get_notifyees(jobdef):
origNotifyeesList = jobdef['notifyees'] if isinstance(jobdef['notifyees'], list) or jobdef['notifyees'] is None else [jobdef['notifyees']]
origNotifyeesList = origNotifyeesList if origNotifyeesList is not None else []
notifyeesList = []
for notifyee in origNotifyeesList:
if 'noreply' not in notifyee:
notifyeesList.append(notifyee)
return notifyeesList
但現在我得到這個錯誤
File "running.py", line 337, in get_notifyees
KeyError: 'notifyees'
================================================================================
10Oct 01:53:03: Exit status: 1
神聖扭曲的邏輯,蝙蝠俠!但嚴重的是,'jobdef'似乎沒有''notifyees'這個鍵的入口......' – 2014-10-10 13:02:49
不是在* 4 *不同的地方訪問'jobdef ['notifyees']',而是使用一個臨時變量排在第一行的因素。 – chepner 2014-10-10 13:10:12
嘗試使用'try'並期望'except'。順便說一句,我不認爲將'notifyees'分配給項目價值和列表是一個好主意。它增加了漏洞,因爲你必須檢查'iterabliity'。 – 2014-10-10 13:40:34