在其中一条评论中,你说:
Somehow I got an Nonetype value, it supposed to be an int, but it’s now a Nonetype object
如果它是你的代码,找出当你得到None当你期望一个数字,并停止发生。
如果它是别人的代码,找出条件下它给出无,并确定一个合理的值使用,与通常的条件代码:
result = could_return_none(x)
if result is None:
result = DEFAULT_VALUE
…甚至…
if x == THING_THAT_RESULTS_IN_NONE:
result = DEFAULT_VALUE
else:
result = could_return_none(x) # But it won't return None, because we've restricted the domain.
没有理由自动使用0在这里 – 依赖于“假”的None的解决方案假设你会想要这个。 DEFAULT_VALUE(如果它甚至存在)完全取决于您的代码的目的。