为什么Python列表添加必须同质? [英] Why must Python list addition be homogenous?

查看:100
本文介绍了为什么Python列表添加必须同质?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

熟悉Python内部原理(CPython或其他实现)的任何人都可以解释为什么要求列表添加是同质的吗:

In [1]: x = [1]

In [2]: x+"foo"
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
C:\Users\Marcin\<ipython-input-2-94cd84126ddc> in <module>()
----> 1 x+"foo"

TypeError: can only concatenate list (not "str") to list

In [3]: x+="foo"

In [4]: x
Out[4]: [1, 'f', 'o', 'o']

为什么上面的x+"foo"不能返回与上述成绩单中的x最终值相同的值?

这个问题来自NPE在这里的问题: Python列表+ =可迭代的行为在任何地方都有记录吗?

更新:我知道不需要异类+=工作(但是可以),同样,也不需要异类+是错误.这个问题是关于为什么选择后一种选择.

过多地说,将序列添加到列表的结果是不确定的.如果这是足够的反对意见,则防止异类+=是有意义的. Update2:特别是,python始终将运算符调用委派给左手操作数,因此不会出现做什么是正确的事情"的问题:左手对象始终管控(除非将它委托给右手).

Update3:对于任何认为这是设计决定的人,请解释(a)为什么没有记录在案;或(b)记录在何处.

Update4:"[1] + (2, )应该返回什么?它应返回与x+=(2, )之后紧随其后最初保存[1]的变量x的值相等的结果值.这个结果是明确的.

解决方案

这些错误报告表明此设计怪癖是一个错误.

问题12318 :

是的,这是预期的行为,是的,这是不一致的.

这种方式已经存在了很长一段时间,Guido表示他不会再这样做了(这在他的遗憾清单中).但是,我们不会通过更改代码来破坏代码(list.__iadd__的作用类似于list.extend).

问题575536 :

目的是list.__iadd__完全对应于 list.extend().无需过度概括 也是list.__add__():这是那些不了解的人的功能 想要对像马丁这样的例子感到惊讶可以避免 通过使用普通的+获取列表.

(当然,我们当中有些人发现此行为非常令人惊讶,包括打开该错误报告的开发人员).

(感谢@Mouad找到了这些).

Can anyone familiar with Python's internals (CPython, or other implementations) explain why list addition is required to be homogenous:

In [1]: x = [1]

In [2]: x+"foo"
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
C:\Users\Marcin\<ipython-input-2-94cd84126ddc> in <module>()
----> 1 x+"foo"

TypeError: can only concatenate list (not "str") to list

In [3]: x+="foo"

In [4]: x
Out[4]: [1, 'f', 'o', 'o']

Why shouldn't the x+"foo" above return the same value as the final value of x in the above transcript?

This question follows on from NPE's question here: Is the behaviour of Python's list += iterable documented anywhere?

Update: I know it is not required that heterogenous += work (but it does) and likewise, it is not required that heterogenous + be an error. This question is about why that latter choice was made.

It is too much to say that the results of adding a sequence to a list are uncertain. If that were a sufficient objection, it would make sense to prevent heterogenous +=. Update2: In particular, python always delegates operator calls to the lefthand operand, so no issue "what is the right thing to do" arises": the left-hand object always governs (unless it delegates to the right).

Update3: For anyone arguing that this is a design decision, please explain (a) why it is not documented; or (b) where it is documented.

Update4: "what should [1] + (2, ) return?" It should return a result value equal with the value of a variable x initially holding [1] immediately after x+=(2, ). This result is well-defined.

解决方案

These bug reports suggest that this design quirk was a mistake.

Issue12318:

Yes, this is the expected behavior and yes, it is inconsistent.

It's been that way for a long while and Guido said he wouldn't do it again (it's in his list of regrets). However, we're not going to break code by changing it (list.__iadd__ working like list.extend).

Issue575536:

The intent was that list.__iadd__ correspond exactly to list.extend(). There's no need to hypergeneralize list.__add__() too: it's a feature that people who don't want to get surprised by Martin-like examples can avoid them by using plain + for lists.

(Of course, there are those of us who find this behaviour quite surprising, including the developer who opened that bug report).

(Thanks to @Mouad for finding these).

这篇关于为什么Python列表添加必须同质?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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