logo

+=

入门。

很多语言里a += 1a = a + 1是等同的,比如 Python:

>>> a = 0
>>> a += 1
>>> a
1
>>> a = a + 1
>>> a
2

放弃?

>>> a = [1, 2, 3]
>>> a += (4,)
>>> a
[1, 2, 3, 4]
>>> a = a + (5,)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can only concatenate list (not "tuple") to list

进阶!

+等同于__add__+=实际等同于__iadd__,当它不存在的时候则调用__add__