TypeError: NoneType object is not iterable in Python

TypeError: NoneType object is not iterable in Python

It means the value of data is None.

Explanation of error: NoneType object is not iterable

In python2, NoneType is the type of None. In Python3 NoneType is the class of None, for example:

>>> print(type(None))     #Python2
<type NoneType>         #In Python2 the type of None is the NoneType type.

>>> print(type(None))     #Python3
<class NoneType>        #In Python3, the type of None is the NoneType class.

Iterating over a variable that has value None fails:

for a in None:
    print(k)     #TypeError: NoneType object is not iterable

Python methods return NoneType if they dont return a value:

def foo():
    print(k)
a, b = foo()      #TypeError: NoneType object is not iterable

You need to check your looping constructs for NoneType like this:

a = None 
print(a is None)              #prints True
print(a is not None)          #prints False
print(a == None)              #prints True
print(a != None)              #prints False
print(isinstance(a, object))  #prints True
print(isinstance(a, str))     #prints False

Guido says only use is to check for None because is is more robust to identity checking. Dont use equality operations because those can spit bubble-up implementationitis of their own. Pythons Coding Style Guidelines – PEP-008

NoneTypes are Sneaky, and can sneak in from lambdas:

import sys
b = lambda x : sys.stdout.write(k) 
for a in b(10): 
    pass            #TypeError: NoneType object is not iterable 

NoneType is not a valid keyword:

a = NoneType     #NameError: name NoneType is not defined

Concatenation of None and a string:

bar = something
foo = None
print foo + bar    #TypeError: cannot concatenate str and NoneType objects

Whats going on here?

Pythons interpreter converted your code to pyc bytecode. The Python virtual machine processed the bytecode, it encountered a looping construct which said iterate over a variable containing None. The operation was performed by invoking the __iter__ method on the None.

None has no __iter__ method defined, so Pythons virtual machine tells you what it sees: that NoneType has no __iter__ method.

This is why Pythons duck-typing ideology is considered bad. The programmer does something completely reasonable with a variable and at runtime it gets contaminated by None, the python virtual machine attempts to soldier on, and pukes up a bunch of unrelated nonsense all over the carpet.

Java or C++ doesnt have these problems because such a program wouldnt be allowed to compile since you havent defined what to do when None occurs. Python gives the programmer lots of rope to hang himself by allowing you to do lots of things that should cannot be expected to work under exceptional circumstances. Python is a yes-man, saying yes-sir when it out to be stopping you from harming yourself, like Java and C++ does.

TypeError: NoneType object is not iterable in Python

TypeError: NoneType object is not iterable in Python

Code: for row in data:
Error message: TypeError: NoneType object is not iterable

Which object is it complaining about? Choice of two, row and data.
In for row in data, which needs to be iterable? Only data.

Whats the problem with data? Its type is NoneType. Only None has type NoneType. So data is None.

You can verify this in an IDE, or by inserting e.g. print data is, repr(data) before the for statement, and re-running.

Think about what you need to do next:
How should no data be represented? Do we write an empty file? Do we raise an exception or log a warning or keep silent?

Leave a Reply

Your email address will not be published. Required fields are marked *