In [26]: l=[]
In [27]: s="asdsad"
In [28]: l+=s
In [29]: l
Out[29]: ['a', 's', 'd', 's', 'a', 'd']
但是,
In [30]: l+s
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/home/xiaohan/code/proteincrawler/id_crawler.py in <module>()
----> 1
2
3
4
5
TypeError: can only concatenate list (not "str") to list
因此,'+=' 和 '+' 中的 + 运算符是不同的。
但我觉得应该是一样的,因为都是plus
是我错了还是幕后发生了什么?
最佳答案
这解释了here .从链接复制:
It's because the
+=
operator is doing the equivalent of calling theextend
method, which treats its argument as a generic sequence, and doesn't enforce type.
关于python - Python加号运算符不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7579719/