What does error TypeError: 'NoneType' object is not iterable
mean?
I am getting it on this Python code:
def write_file(data, filename): # creates file and writes list to it with open(filename, 'wb') as outfile: writer = csv.writer(outfile) for row in data: # ABOVE ERROR IS THROWN HERE writer.writerow(row)
The error “TypeError: 'NoneType' object is not iterable” occurs when you try to iterate over a NoneType object. Objects like list, tuple, and string are iterables, but not None. To solve this error, ensure you assign any values you want to iterate over to an iterable object.
The Python "TypeError: argument of type 'NoneType' is not iterable" occurs when we use the membership test operators (in and not in) with a None value. To solve the error, correct the assignment of the variable that stores None or check if it doesn't store None .
The Python "TypeError: 'NoneType' object is not iterable" occurs when we try to iterate over a None value. To solve the error, figure out where the variable got assigned a None value and correct the assignment or check if the variable doesn't store None before iterating.
One way to avoid this error is to check before iterating on an object if that object is None or not. In addition, another way to handle this error: Python nonetype object is not iterable is to write the for loop in try-except block. Thirdly, it is to explicitly assign an empty list to the variable if it is None .
It means the value of data
is None
.
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.
for a in None: print("k") #TypeError: 'NoneType' object is not iterable
def foo(): print("k") a, b = foo() #TypeError: 'NoneType' object is not iterable
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. Don't use equality operations because those can spit bubble-up implementationitis of their own. Python's Coding Style Guidelines - PEP-008
import sys b = lambda x : sys.stdout.write("k") for a in b(10): pass #TypeError: 'NoneType' object is not iterable
a = NoneType #NameError: name 'NoneType' is not defined
None
and a string:bar = "something" foo = None print foo + bar #TypeError: cannot concatenate 'str' and 'NoneType' objects
Python's 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 Python's virtual machine tells you what it sees: that NoneType has no __iter__
method.
This is why Python's 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++ doesn't have these problems because such a program wouldn't be allowed to compile since you haven't 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With