延伸反应不一致 [英] Inconsistent reaction to extend
问题描述
大师,在我想要将此作为一个错误的信号之前,也许
你可能会说服我应该如此。如果我输入
l = range(4)
l.extend([1,2])
l给[0,1,2,3,1,2],还有什么...
另一方面,试试
p = range(4).extend([1,2])
然后,p没有值(NoneType)。
附加行为是类似的。我没有尝试其他方法,但
我怀疑它不会改善。
为什么?
似乎那里已经有一些关于一致性的讨论了。
有人制作了这个例子:h = {}。update(l)哪个不行,
但我不是订阅了这个nsgr,我无法理解这件事。
Jerzy Karczmarczuk
Gurus, before I am tempted to signal this as a bug, perhaps
you might convince me that it should be so. If I type
l=range(4)
l.extend([1,2])
l gives [0,1,2,3,1,2], what else...
On the other hand, try
p=range(4).extend([1,2])
Then, p HAS NO VALUE (NoneType).
With append the behaviour is similar. I didn''t try other methods, but
I suspect that it won''t improve.
WHY?
It seems that there was already some discussion about consistency and
somebody produced the example: h = {}.update(l) which didn''t work,
but I wasn''t subscribed to this nsgr, I couldn''t follow this affair.
Jerzy Karczmarczuk
推荐答案
Jerzy Karczmarczuk kirjoitti:
Jerzy Karczmarczuk kirjoitti:
另一方面,尝试
p = range(4).extend([1,2 ])
然后,p没有值(NoneType)。
使用追加行为是类似的。我没有尝试其他方法,但是我怀疑它不会改善。
为什么?
On the other hand, try
p=range(4).extend([1,2])
Then, p HAS NO VALUE (NoneType).
With append the behaviour is similar. I didn''t try other methods, but
I suspect that it won''t improve.
WHY?
range(4)返回一个列表,Python的list.extend()返回None。延伸
是一个就地操作。
- timo
range(4) returns a list and Python''s list.extend() returns None. Extend
is a in-place operation.
-- timo
Jerzy Karczmarczuk写道:
Jerzy Karczmarczuk wrote:
大师,在我想要将此作为一个错误的信号之前,也许
你可能会说服我应该如此。
它不是一个错误,没有人应该让你相信任何
的东西;尽管你可以从阅读某些切片线程中思考,
这个邮件列表不是一个参数诊所。
如果我打字
l =范围(4)
l.extend([1,2])
l给出[0,1,2,3,1,2],还有什么......
另一方面,尝试
p = range(4).extend([1,2])
然后,p HAS NO VALUE(NoneType) 。
(脚注:无是Python中的值。它可用于表示无值,
但它也可用于其他事情)
附加行为类似。我没有尝试其他方法,但是我怀疑它没有改善。
为什么?
Gurus, before I am tempted to signal this as a bug, perhaps
you might convince me that it should be so.
it''s not a bug, and nobody should have to convince you about any-
thing; despite what you may think from reading certain slicing threads,
this mailing list is not an argument clinic.
If I type
l=range(4)
l.extend([1,2])
l gives [0,1,2,3,1,2], what else...
On the other hand, try
p=range(4).extend([1,2])
Then, p HAS NO VALUE (NoneType).
(footnote: None is a value in Python. it can be used to represent "no value",
but it can also be used for other things)
With append the behaviour is similar. I didn''t try other methods, but
I suspect that it won''t improve.
WHY?
因为你保存了extend的返回值,而不是range和extend,
返回None。
if你把它拆分好
l =范围(4)
p = l.extend([1,2])
应该很明显l和p并不一定包含同样的东西。
< / F>
because you''re saving the return value of "extend", not "range", and "extend"
returns None.
if you split it up like this
l = range(4)
p = l.extend([1, 2])
it should be obvious that "l" and "p" doesn''t necessarily contain the same thing.
</F>
Jerzy Karczmarczuk写道:
Jerzy Karczmarczuk wrote:
大师,在我想要将此作为一个错误的信号之前,也许
你可能会说服我应该如此。如果我输入
l = range(4)
l.extend([1,2])
l给出[0,1,2,3, 1,2],还有什么...
另一方面,尝试
p = range(4).extend([1,2])
然后,p没有值(NoneType)。
附加行为类似。我没有尝试其他方法,但是我怀疑它没有改善。
为什么?
Gurus, before I am tempted to signal this as a bug, perhaps
you might convince me that it should be so. If I type
l=range(4)
l.extend([1,2])
l gives [0,1,2,3,1,2], what else...
On the other hand, try
p=range(4).extend([1,2])
Then, p HAS NO VALUE (NoneType).
With append the behaviour is similar. I didn''t try other methods, but
I suspect that it won''t improve.
WHY?
..append(),. extend(),. sort()(以及字典的.update())
都是* * *效果是就地修改对象的方法。
他们返回None作为提示,他们确实修改了对象而不是
复制对象然后修改副本。从常见问题[1]与
相对于.sort():
"""这样,你就不会被愚弄无意中覆盖列表
当你需要一个分类副本但还需要保留未分类的版本
周围。""
>
[1]
http://www.python.org/doc/faq/genera...he-sorted-list
-
Robert Kern
rk***@ucsd.edu
在地狱的地方,草地长得很高
梦想的坟墓是否能够死亡。
- - Richard Harter
..append(), .extend(), .sort() (as well as .update() for dictionaries)
all are methods whose *only* effect is to modify the object in-place.
They return None as a reminder that they do modify the object instead of
copying the object and then modifying the copy. From the FAQ[1] with
respect to .sort():
"""This way, you won''t be fooled into accidentally overwriting a list
when you need a sorted copy but also need to keep the unsorted version
around."""
[1]
http://www.python.org/doc/faq/genera...he-sorted-list
--
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
这篇关于延伸反应不一致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!