【发布时间】:2015-10-13 17:46:54
【问题描述】:
一个列表包含多个NoneType 元素。要跳过NoneType,
for item in list :
if item is not None :
fp.write(item + '\n')
#OR
for item in list :
try :
fp.write(item + '\n')
except :
pass
哪个更好,为什么?
【问题讨论】:
-
那么第二个将跳过所有不支持与字符串连接的类型。
-
你的最终目标是什么?
-
您将在此答案stackoverflow.com/questions/3845423/…中找到有关该主题的一些信息
-
下面的答案都没有回答你的问题吗?
-
考虑忽略异常时的重要读物:Why is “except: pass” a bad programming practice? 所以,正如 Tim 在下面提到的,绝对应该这样做
except TypeError: