Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
类型错误:';非类型';对象在Python中是不可编辑的_Python_Nonetype - Fatal编程技术网

类型错误:';非类型';对象在Python中是不可编辑的

类型错误:';非类型';对象在Python中是不可编辑的,python,nonetype,Python,Nonetype,errorTypeError:“NoneType”对象不可编辑是什么意思 我在这段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

error
TypeError:“NoneType”对象不可编辑是什么意思

我在这段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)

这意味着
data
的值是
None

您正在使用如下参数调用write\u文件:

write_file(foo, bar)

但是您没有正确定义“foo”,或者您的代码中有一个输入错误,因此它正在创建一个新的空变量并将其传入。

这意味着数据变量正在传递None(类型为NoneType),它的等价物是空的。因此,它不能像您试图做的那样作为列表进行编辑。

代码:对于数据中的行:
错误消息:
TypeError:“非类型”对象不可编辑

它在抱怨什么?两种选择,
数据
。 对于数据中的行,在
中,哪个需要可编辑?仅
数据

数据有什么问题?其类型为
NoneType
。只有
None
具有类型
NoneType
。所以
数据是无的

您可以在IDE中验证这一点,或者在
for
语句的
之前插入例如
print“data is”,repr(data)
,然后重新运行

考虑下一步需要做什么: “无数据”应如何表示?我们写一个空文件吗?我们是否引发异常、记录警告或保持沉默?

错误解释:“非类型”对象不可编辑 在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.
Python方法如果不返回值,则返回NoneType: 您需要检查非类型的循环构造,如下所示: Guido说,只使用
is
检查
None
,因为
is
对身份检查更可靠。不要使用相等操作,因为这些操作可能会自己产生气泡

NoneType是狡猾的,可以从lambdas潜入: 非类型不是有效的关键字:
None
和字符串的串联: 这是怎么回事? Python的解释器将您的代码转换为pyc字节码。Python虚拟机处理字节码时,遇到了一个循环构造,该构造表示迭代一个不包含任何字节码的变量。该操作是通过调用None上的
\uuu iter\uu
方法来执行的

None没有定义
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu

这就是为什么意识形态被认为是坏的。程序员用一个变量做了一些完全合理的事情,在运行时它被一个变量都没有污染,python虚拟机试图坚持下去,并且吐出了一堆无关的废话


java或C++没有这些问题,因为这样的程序不允许编译,因为在没有发生的时候,你还没有定义要做什么。Python允许程序员做很多在特殊情况下不应该工作的事情,这给了程序员很大的束缚。Python是一个赞成的人,当你阻止你伤害自己时,说“是的”先生,像java和C++一样。

另一个可能产生这个错误的事情是,当你设置的东西等于函数的返回值,却忘记实际返回任何东西。 例如:

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)
这是一个很难发现的错误,因为如果在一次迭代中row变量恰好为None,也会产生错误。发现它的方法是跟踪在最后一行失败,而不是在函数内部


如果一个函数只返回一个变量,我不确定是否会产生错误。。。我怀疑这个例子中的错误“'NoneType'对象在Python中不可iterable”实际上意味着“嘿,我正试图迭代返回值,以便将它们按顺序分配给这三个变量,但我只得到一个值来迭代”

对于我来说,这是一个使用my而不是Python 3的情况

忘记了
def
函数末尾的
return
关键字

已经有几个月没有认真编写Python3了。认为例程中计算的最后一条语句是按照Groovy(或Rust)方式返回的

经过几次迭代,查看堆栈跟踪,插入
try:。。。除了TypeError:…
阻止调试/单步执行代码以找出错误所在


消息的解决方案当然不会让错误跳到我身上。

如果没有异常,请继续循环

例如:

   a = None
   if a is None:
       continue
   else:
       print("do something")

这可以是来自DB或excel文件的任何iterable。

如果只是像一个空列表一样迭代就好了。。。将为更干净的代码和更少的错误而努力checking@deltanine我想这会使很多问题更难发现。我很高兴没有什么不同于一个空的iterable。如果您想要描述您的行为,只需对数据中的行使用
,或[]:
(a)混淆
NoneType
None
(b)认为
NameError:name'NoneType'没有定义
TypeError:cannot concatenate'str'和'NoneType'对象
TypeError相同:'NoneType'对象不可移植
(c)Python和java之间的比较是“一堆无关的废话”。。。如果将
null
传递给一个需要任何集合类型的函数,Java将遇到本质上相同的问题。C++对于C++代码NoLPTPt<代码>会有相同的问题(但通常只是在Sebug中没有报告原因)。(无可否认,好的C++很少使用指针,但是你演示的是坏的Python,坏的C++也会在运行时从零上死亡)。Python在这里做的是正确的事情;它不是“当兵”,而是在你尝试使用
None
处理任何
Nonea = 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
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
bar = "something"
foo = None
print foo + bar    #TypeError: cannot concatenate 'str' and 'NoneType' objects
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)
   a = None
   if a is None:
       continue
   else:
       print("do something")