简体   繁体   English

Python:表达式返回不同的响应a = a + 1与a + = 1

[英]Python: Expression returns Different Response a=a+1 vs a+=1

Note: I know python is strongly typed and we wont concatenate 2 different types. 注意:我知道python是强类型的,我们不会串联2种不同的类型。 but accidentally i have tried , 但是我偶然地尝试过

I got different result, 我得到了不同的结果,

a = []
>>>a=a+1 #it gives me expected concatenate error
TypeError: can only concatenate list (not "int") to list


>>>a+=1 #but this wasn't
TypeError: 'int' object is not iterable

This is because operator + and operator += is not same magic function implement in list class. 这是因为运算符+和运算符+ =在列表类中不是相同的魔术函数实现。

__iadd__() ## Map to += Operator
__add__() ## Map to + Operator


That means you trigger different magic function of list object. 这意味着您将触发列表对象的其他魔术功能。

From what I gather while doing experimentation is that. 根据我在进行实验时收集到的信息,得出的结论是。

  1. a = a + 1 is concatenating a list and an integer, but as we know + operator or concatenation requires two lists, hence the error we get is TypeError: can only concatenate list (not "range") to list , as we can see below a = a + 1正在连接一个列表和一个整数,但是我们知道+运算符或连接需要两个列表,因此我们得到的错误是TypeError: can only concatenate list (not "range") to list ,如我们所见下面
In [43]: [] + 1                                                                 
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-43-67dee89361ae> in <module>
----> 1 [] + 1

TypeError: can only concatenate list (not "int") to list
In [47]: [] + []                                                                
Out[47]: []
  1. In a += 1 , we internally call a.extend(1) as per the docs: https://docs.python.org/3/library/stdtypes.html#mutable-sequence-types a += 1 ,我们根据docs内部调用a.extend(1)https : a.extend(1)

s.extend(t) or s += t . s.extend(t)或s + = t。
extends s with the contents of t (for the most part the same as s[len(s):len(s)] = t) 用t的内容扩展s(在大多数情况下与s [len(s):len(s)] = t相同)

Also from the docs at: https://docs.python.org/3/tutorial/datastructures.html 也来自docs: https : //docs.python.org/3/tutorial/datastructures.html

list.extend(iterable) list.extend(iterable)
Extend the list by appending all the items from the iterable. 通过附加来自iterable的所有项来扩展列表。 Equivalent to a[len(a):] = iterable. 等效于a [len(a):] =可迭代的。

So 1 is being treated as an iterable, but since int is not an iterable, we get the error TypeError: 'int' object is not iterable as we can see below 因此1被视为可迭代的,但是由于int不是可迭代的,因此我们收到错误TypeError: 'int' object is not iterable ,如下所示

In [49]: a = []                                                                 

In [50]: a.extend(1)                                                            
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-50-4671950943e4> in <module>
----> 1 a.extend(1)

TypeError: 'int' object is not iterable

As stated the two operators do not map to the same magic function. 如前所述,两个运算符没有映射到相同的魔术函数。 And the two have different implementations. 两者具有不同的实现。 This has some interesting consequences. 这会产生一些有趣的结果。 the normal plus ( + or __add__ ) expects another list to concatenate whereas in-place add ( += or __iadd__ ) is more allowing and can take any iterator and make it into a list before adding: 普通的加号( +__add__ )期望将另一个列表连接起来,而就地添加( +=__iadd__ )更允许,并且可以在添加之前将任何迭代器放入列表中:

a = []
a = a + range(2)
TypeError: can only concatenate list (not "range") to list

a += range(2)
[0, 1]

I don't know why this is though. 我不知道为什么会这样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM