奇怪的错误 [英] Odd Errors

查看:61
本文介绍了奇怪的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我跑步时:

#!/ usr / bin / python

行=列表()


而1 :

试试:

inLine = raw_input()

lines = lines.append(inLine)
除了EOFError之外的


休息


我得到:

Traceback(最近一次调用最后一次):

档案" ./ foobar.py",第7行,< module>

lines = lines.append(inLine)

AttributeError:''NoneType''对象有没有属性''追加''

解决方案

问题在于:


* * * * lines = lines.append(inLine)



列表的追加方法就地修改列表,它没有

返回列表的副本,并附加新元素。事实上,它是
返回None,然后它将标签''lines''附加到,所以下一次

迭代通过它尝试调用None.append .. 。


将行换成:


lines.append(inLine)


9月28日,7:13 * pm,alex23< wuwe ... @ gmail.comwrote:


问题在于:
< blockquote class =post_quotes>
* * * * lines = lines.append(inLine)



列表的追加方法修改列表 - 这个地方,它没有
返回一个附加了新元素的列表副本。事实上,它是
返回None,然后它将标签''lines''附加到,所以下一次

迭代通过它尝试调用None.append .. 。


将行替换为:


* * lines.append(inLine)



你是否想要从屋顶上尖叫,''追加''通过

副作用操作!"?


" Aaron \Castironpi \贝迪" < castiro ... @ gmail.comwrote:


你是否想要从屋顶尖叫,''追加''由
操作
副作用!?



我很生气,我不会再在原地变异了!


When I run:
#!/usr/bin/python
lines = list()

while 1:
try:
inLine = raw_input()
lines = lines.append(inLine)
except EOFError:
break

I get:
Traceback (most recent call last):
File "./foobar.py", line 7, in <module>
lines = lines.append(inLine)
AttributeError: ''NoneType'' object has no attribute ''append''

解决方案

The problem is with this:

* * * * lines = lines.append(inLine)

The append method of a list modifies the list in-place, it doesn''t
return a copy of the list with the new element appended. In fact, it
returns None, which it then attaches the label ''lines'' to, so the next
iteration through it tries to call None.append...

Replace the line with:

lines.append(inLine)


On Sep 28, 7:13*pm, alex23 <wuwe...@gmail.comwrote:

The problem is with this:

* * * * lines = lines.append(inLine)


The append method of a list modifies the list in-place, it doesn''t
return a copy of the list with the new element appended. In fact, it
returns None, which it then attaches the label ''lines'' to, so the next
iteration through it tries to call None.append...

Replace the line with:

* * lines.append(inLine)

Do you ever want to scream from the rooftops, "''append'' operates by
side-effect!"?


"Aaron \"Castironpi\" Brady" <castiro...@gmail.comwrote:

Do you ever want to scream from the rooftops, "''append'' operates by
side-effect!"?

"I''m mad as hell, and I''m not going to mutate in-place anymore!"


这篇关于奇怪的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆