TypeError: ‘NoneType’ object is not iterable

我会带着你远行 2023-10-17 09:23 50阅读 0赞

TypeError: ‘NoneType’ object is not iterable 这个错误提示一般发生在将None赋给多个值时。

  1. def myprocess():
  2. a == b
  3. if a != b:
  4. return True, value;
  5. flag, val = myprocess()

在判断语句中,当if条件不满足,并且没有else语句时,函数默认返回None。

在没有return语句时,python也默认会返回None

调用时,将None赋给多个值时,会出现提示:TypeError: ‘NoneType’ object is not iterable

本例中,flag和val都被赋予了None(因为if条件不满足,所以默认返回None)就出现了这样的错误提示。

所以函数返回值一定要考虑到条件分支的覆盖

发表评论

表情:
评论列表 (有 0 条评论,50人围观)

还没有评论,来说两句吧...

相关阅读