错误TypeError: 'NoneType' object is not iterable是什么意思?
我在这个Python代码上得到它:
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)
Run Code Online (Sandbox Code Playgroud)
van*_*nza 179
这意味着"数据"是无.
Eri*_*ski 66
在python2中,NoneType是None的类型.在Python3中,NoneType是None类,例如:
>>> 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.
Run Code Online (Sandbox Code Playgroud)
for a in None:
print("k") #TypeError: 'NoneType' object is not iterable
Run Code Online (Sandbox Code Playgroud)
def foo():
print("k")
a, b = foo() #TypeError: 'NoneType' object is not iterable
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
Guido说只用于is检查,None因为is身份检查更加健壮.不要使用平等操作,因为那些可以吐出自己的冒泡实施. Python的编码风格指南 - PEP-008
import sys
b = lambda x : sys.stdout.write("k")
for a in b(10):
pass #TypeError: 'NoneType' object is not iterable
Run Code Online (Sandbox Code Playgroud)
a = NoneType #NameError: name 'NoneType' is not defined
Run Code Online (Sandbox Code Playgroud)
None和字符串:bar = "something"
foo = None
print foo + bar #TypeError: cannot concatenate 'str' and 'NoneType' objects
Run Code Online (Sandbox Code Playgroud)
Python的解释器将您的代码转换为pyc字节码.Python虚拟机处理了字节码,它遇到了一个循环结构,表示迭代包含None的变量.通过__iter__在None上调用方法来执行该操作.
没有__iter__定义方法,所以Python的虚拟机会告诉你它看到了什么:NoneType没有__iter__方法.
这就是为什么Python的鸭子打字意识形态被认为是坏的.程序员用变量做了一些完全合理的事情,并且在运行时它被None污染了,python虚拟机试图继续攻击,并在整个地毯上掀起一堆无关的废话.
Java或C++没有这些问题,因为这样的程序不允许编译,因为你没有定义在None发生时要做什么.Python为程序员提供了很多绳索,可以让你做很多事情,这些事情在特殊情况下是不可能的.Python是一个肯定的人,说是先生,当它阻止你自己伤害时,就像Java和C++一样.
Joh*_*hin 63
代码:for row in data:
错误消息:TypeError: 'NoneType' object is not iterable
抱怨哪个对象?选择两个,row和data.在for row in data哪,需要可迭代?只有data.
有什么问题data?它的类型是NoneType.只有None类型NoneType.所以data is None.
您可以在IDE中进行验证,也可以print "data is", repr(data)在for语句之前插入并重新运行.
想想下一步你需要做什么: 如何表示"无数据"?我们写一个空文件吗?我们是否提出异常或记录警告或保持沉默?
gun*_*gor 17
另一件可能产生此错误的事情是当你设置的东西等于函数的返回值时,却忘了实际返回任何东西.
例:
def foo(dict_of_dicts):
for key, row in dict_of_dicts.items():
for key, inner_row in row.items():
Do SomeThing
#Whoops, forgot to return all my stuff
return1, return2, return3 = foo(dict_of_dicts)
Run Code Online (Sandbox Code Playgroud)
这是一个很难发现的错误,因为如果在其中一个迭代上行变量为None,也会产生错误.发现它的方法是跟踪在最后一行而不是在函数内部失败.
如果你只从一个函数返回一个变量,我不确定是否会产生错误...我怀疑错误"'NoneType'对象在Python中不可迭代"在这种情况下实际上暗示"嘿,我正在尝试迭代返回值,按顺序将它们分配给这三个变量,但我只得到无迭代"
小智 8
这意味着数据变量传递None(类型为NoneType),它等效于什么.因此,它无法像列表一样进行迭代,就像您尝试的那样.
你用这样的参数调用write_file:
write_file(foo, bar)
Run Code Online (Sandbox Code Playgroud)
但是你没有正确定义'foo',或者你的代码中有一个拼写错误,所以它创建了一个新的空变量并将其传入.