python - Python加号运算符不一致

标签 python operators

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 the extend method, which treats its argument as a generic sequence, and doesn't enforce type.

关于python - Python加号运算符不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7579719/

相关文章:

python - 使用Python找出两个excel中丢失的交易

python - 使用 ephem 计算 "Solar Noon",转换为本地时间

c++ - 为什么 C 选择某些运算符作为它们的符号?

c++ - 为什么不使用强制转换语法调用 "operator void"?

c++ - 二元 boolean 运算符是否具有结合性?

c# - 有没有办法制作一个自JIT编译的程序?

python - 使用 matplotlib 绘制箱线图

python - 遍历给定目录中的 python 文件并导入它们?

operators - 在csh中,为什么4 - 3 + 1 == 0?

ActionScript 日期比较