这是一个错误吗? [英] Is this a bug?

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

问题描述



我已经找到了一个我可以向自己解释的案例*为什么*它会发生,在python 2.3和python 2.4下都是
,但是以下不一致使我

想知道我是否应该将其记录为错误:


首先,这种行为并非出乎意料:
< blockquote class =post_quotes>

a = [" hello"]
a = a +" world"
Traceback(最近一次调用最后一次):

文件"< stdin>",第1行,在?

TypeError:只能连接列表(不是str)列出


这是我所期待的那样。


但是如果我们这样做只是略有不同:

a = [" hello"]
a + =" world"
a



[''hello'',''w'','''',''r'','l'',''d'']


我们的行为完全不同。这让我觉得它是一个错误 - 我应该把它作为一个原因记录下来吗?或者这个行为有充分理由吗?


问候,

迈克尔。

解决方案

Michael Sparks写道:



我已经找到了一个我可以向自己解释的原因*为什么*它发生了,在python 2.3和python 2.4下都有,但是下面的不一致让我想知道我是否应该登录它是一个错误:

首先,这种行为并非出乎意料:

a = [ " hello"]
a = a +" world"
跟踪(最近一次呼叫最后一次):
文件"< stdin>",第1行,在?
TypeError:只能连接列表(不是str)到列表

这是我所期待的。

但是,如果我们这样做只是稍微不同的:

a = [" hello"]
a + =世界
a



[''hello'',''w'','o'',''r'' ,'''',''d'']

我们的行为完全不同。这让我觉得它是一个错误 - 我应该将它作为一个错误记录下来,还是有充分的理由表明这种行为?




它符合使用a.extend(world)这就是+ =是
糖的含义。


在[1]中:a = [''你好'']


在[2]中:a.extend(world)


在[3]中:a br />
Out [3]:[''hello'',''w'','o'',''r'','l'','d'']


。这是一个很好的事情.extend()在没有明确的

转换为列表的情况下接受任何迭代。我认为

行为传递给+ =只是一个小麻烦。


-

Robert Kern
rk***@ucsd.edu


In草地长高的地狱之地

梦想的坟墓是否已经死亡。

- Richard Harter


当你使用a = a +'''''时,因为

不同的类型,python认为它是一个错误。


但是当你使用+ ='''''时,
python会将右边改为列表(因为a是一个列表)。所以,当

你的代码变成:

a + ='''世界''#a + = list(''world'')


如果你坚持使用追加而不是+ =,那么你真的很有帮助

操作清单


Pujo


2005-04-24,Michael Sparks< za ***** @ thwackety.com>写道:

a = [" hello"]
a = a +" world"回溯(最近一次调用最后一次):
文件"< stdin>",第1行,在?
TypeError:只能连接列表(不是str)列表
但是,如果我们这样做略有不同:
a = [" hello]
a + =" world"
a


[ ''你好',''w'','o'',''r'','''','''''

我们的行为完全不同。这让我觉得它是一个错误 - 我应该把它记录为一个,还是这个行为有充分的理由?




我认为它是一个无论行为的原因是什么错误:


1)它没有做出合理的用户期望。


2)它不会做文档所说的那样。

根据语言参考,


增强的赋值表达式如x + = 1可以

重写为x = x + 1来实现类似的,但不是

完全相同的效果。在增强版中,x只评估一次



我不认为你发布的两个结果是相似。


-

格兰特爱德华兹格兰特哇! TAILFINS !! ...点击......



visi.com


Hi,
I''ve hit a corner case that I can explain to myself *why* it happens, both
under python 2.3 and python 2.4, but the following inconsistency makes me
wonder if I should log it as a bug:

First the behaviour that isn''t unexpected:

a=["hello"]
a = a + "world" Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: can only concatenate list (not "str") to list
Which is pretty what I''d expect.

However if we do this just slightly differently:
a = ["hello"]
a += "world"
a


[''hello'', ''w'', ''o'', ''r'', ''l'', ''d'']

We get completely different behaviour. This strikes me as a bug - should I
log it as one, or is there a good reason for this behaviour?

Regards,
Michael.

解决方案

Michael Sparks wrote:

Hi,
I''ve hit a corner case that I can explain to myself *why* it happens, both
under python 2.3 and python 2.4, but the following inconsistency makes me
wonder if I should log it as a bug:

First the behaviour that isn''t unexpected:

a=["hello"]
a = a + "world"
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: can only concatenate list (not "str") to list
Which is pretty what I''d expect.

However if we do this just slightly differently:

a = ["hello"]
a += "world"
a



[''hello'', ''w'', ''o'', ''r'', ''l'', ''d'']

We get completely different behaviour. This strikes me as a bug - should I
log it as one, or is there a good reason for this behaviour?



It''s consistent with using a.extend("world") which is what the += is
sugar for.

In [1]:a = [''hello'']

In [2]:a.extend("world")

In [3]:a
Out[3]:[''hello'', ''w'', ''o'', ''r'', ''l'', ''d'']

It''s a *good* thing that .extend() takes any iterable without explicit
conversion to a list. I think that it''s just a minor annoyance that the
behavior passes on to +=.

--
Robert Kern
rk***@ucsd.edu

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter


when you use a = a + ''world'' python sees it as an error because of
different type.

But when you use a += ''world''
python will change the right into list (because a is a list). So when
you''re code become:
a += ''world'' # a += list(''world'')

It really helpfull if you stick to use append instead of += when you
operate list

Pujo


On 2005-04-24, Michael Sparks <za*****@thwackety.com> wrote:

a=["hello"]
a = a + "world" Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: can only concatenate list (not "str") to list However if we do this just slightly differently:
a = ["hello"]
a += "world"
a


[''hello'', ''w'', ''o'', ''r'', ''l'', ''d'']

We get completely different behaviour. This strikes me as a bug - should I
log it as one, or is there a good reason for this behaviour?



I think it''s a bug regardless of the reason for the behavior:

1) It doesn''t do what a reasonable user expects.

2) It doesn''t do what the documentation says it will.
According to the language reference,

An augmented assignment expression like x += 1 can be
rewritten as x = x + 1 to achieve a similar, but not
exactly equal effect. In the augmented version, x is only
evaluated once.

I don''t consider the two results you posted "similar".

--
Grant Edwards grante Yow! TAILFINS!!...click...
at
visi.com


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

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