python list的+=操作

>>> l=range(3)
>>> t=(3,4,5)
>>> l
[0,
1, 2]
>>> t
(
3, 4, 5)
>>> l.extend(t)
>>> l
[0,
1, 2, 3, 4, 5]
>>> l+=t
>>> l
[0,
1, 2, 3, 4, 5, 3, 4, 5]

看来list对象的+=操作和extend方法有异曲同工之处.

如果我们直接l+t,就会报错,因为+不会对不同类型的object进行操作.

>>> l+t

Traceback (most recent call last):
File
"<pyshell#17>", line 1, in <module>
l
+t
TypeError: can only concatenate list (
not "tuple") to list

posted @ 2011-06-09 10:55  john2000  阅读(1136)  评论(0编辑  收藏  举报