+=
白鹤亮翅
很多语言里a += 1和a = 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__。
古法手工编程:从入门到放弃
