看起来在Python中,list += x适用于任何可迭代的x:
In [6]: l = []
In [7]: l += [1]
In [8]: l += (2, 3)
In [9]: l += xrange(5)
In [10]: l
Out[10]: [1, 2, 3, 0, 1, 2, 3, 4]
Run Code Online (Sandbox Code Playgroud)
这种行为记录在哪里吗?
有人可以解释一下这种行为:
>>> a = {'hello':'world' , 'good':'food'}
>>> b = [1,2]
>>> b = b + a
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can only concatenate list (not "dict") to list
>>> b += a
>>> b
[1, 2, 'good', 'hello'] <--- Why do the keys get added when dict cannot be added to a list
>>>
Run Code Online (Sandbox Code Playgroud)