1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > python nonetype转换float_python如何将Nonetype转换为int或字符串

python nonetype转换float_python如何将Nonetype转换为int或字符串

时间:2023-08-17 23:57:00

相关推荐

python nonetype转换float_python如何将Nonetype转换为int或字符串

在其中一条评论中,你说:

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(如果它甚至存在)完全取决于您的代码的目的。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。