【发布时间】:2014-03-04 16:55:29
【问题描述】:
运行以下脚本时
dictlist = [
{'a': 'hello world', 'b': 'my name is Bond'},
{'a': 'bonjour monde'}
]
for d in dictlist:
if 'Bond' not in d.get('b'):
print d
我希望输出为空(第一个 dict 不匹配,第二个缺少密钥 'b')但我收到错误:
Traceback (most recent call last):
File "C:/dev/mytest.py", line 7, in <module>
if 'Bond' not in d.get('b'):
TypeError: argument of type 'NoneType' is not iterable
我很困惑:为什么在我不迭代时会出现argument of type 'NoneType' is not iterable 错误(至少在那一行)?
我确信这是一个明显的错误,但我查看代码的次数越多,我看到它的机会就越少:)
【问题讨论】: