Tag Archives: Nonetype’ object is not iterable

Fast locating nonetype ‘object is not Iterable in Python

One word solution

There are two ways to solve such problems

1. Check whether the list in all for loops has none

2. Check all scenarios that call function to return multiple parameters, check whether there are branches inside the function, and whether each branch has a return value

Verification

# coding: utf-8
value = 0

def test():
    if value == 1:
        a = b = 1
        return a, b

def case1():
    a, b = test()
    print a

def case2():
    items = None
    for item in items:
        print item

if __name__ == '__main__':
    print case2()



Error information:

TypeError: ‘NoneType’ object is not iterable

Summary

The most common error scenario of ‘nonetype’ object is not Iterable is that the list in the for loop is none

When assigning none to multiple values, a prompt will also appear: typeerror: ‘nonetype’ object is not Iterable

Function return value must consider the coverage of conditional branch

When there is no return statement, python returns none by default

Case 1 is easy to ignore

therefore, if this error occurs, we should check from the above two scenarios, such as whether there is a value of none used in the loop, whether the called function has branches, and whether each branch has a return value

Reference

https://www.cnblogs.com/zhaijiahui/p/8391701.html