在 Python 中, a += b 并不总是等价于 a = a + b

2019-01-29 21:27:43 +08:00
 itskingname

大家经常在一些博客中看到这样的说法:

a += 1

等价于

a = a + 1

这种说法实际上并不准确。

我们来看一个例子:

>>> a = [1, 2, 3]
>>> a += (4,)
>>> a
[1, 2, 3, 4]

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

这里报错了,说明a += ba = a + b并不是完全等价的。

请点击查看完整内容:在 Python 中,a += b 并不总是等价于 a = a + b

3714 次点击
所在节点    推广
30 条回复
misaka19000
2019-01-29 21:44:35 +08:00
不错,学习了
OysterQAQ
2019-01-29 21:53:39 +08:00
学习了,查了下,在 java 中从本质讲这两者确实是两码事
sdijeenx
2019-01-29 22:22:07 +08:00
是这样的。

These methods are called to implement the augmented arithmetic assignments (+=, -=, *=, @=, /=, //=, %=, **=, <<=, >>=, &=, ^=, |=). These methods should attempt to do the operation in-place (modifying self) and return the result (which could be, but does not have to be, self). If a specific method is not defined, the augmented assignment falls back to the normal methods. For instance, if x is an instance of a class with an __iadd__() method, x += y is equivalent to x = x.__iadd__(y) . Otherwise, x.__add__(y) and y.__radd__(x) are considered, as with the evaluation of x + y. In certain situations, augmented assignment can result in unexpected errors (see Why does a_tuple[i] += [‘ item ’] raise an exception when the addition works?), but this behavior is in fact part of the data model.
https://docs.python.org/3/reference/datamodel.html


Why does a_tuple[i] += [‘ item ’] raise an exception when the addition works?
This is because of a combination of the fact that augmented assignment operators are assignment operators, and the difference between mutable and immutable objects in Python.

This discussion applies in general when augmented assignment operators are applied to elements of a tuple that point to mutable objects, but we ’ ll use a list and += as our exemplar.
https://docs.python.org/3/faq/programming.html#faq-augmented-assignment-tuple-error
rekulas
2019-01-29 22:46:10 +08:00
这个,我觉得别人说的是数值吧,你这个改成了 dict 相加不符合别人的原意了?(非 python 研究者,这似乎是 dict 吧)
bumz
2019-01-29 22:57:10 +08:00
本来就不是一回事

只不过对于值语义的类型,这二者的结果期望是等价的

list 是引用语义的类型,不等价
whwq2012
2019-01-29 22:57:59 +08:00
偷换概念。。一般说的+=是数字运算,又不是其他对象。。
helloworld000
2019-01-29 23:29:32 +08:00
现在从 python 开始学编程的人都已经不知道 数据类型这么个东西了吗。。。。
mario85
2019-01-30 00:12:50 +08:00
推公众号的吧。
@Livid
itskingname
2019-01-30 00:20:47 +08:00
@mario85 已 block
pkookp8
2019-01-30 00:22:49 +08:00
这算是运算符重载吧
你想写成什么样都可以
lance6716
2019-01-30 00:45:55 +08:00
@itskingname 竟然还反向 b,牛逼牛逼
junmoxiao
2019-01-30 00:54:45 +08:00
,,,,,这,,,水平有点低呀
ETiV
2019-01-30 01:30:30 +08:00
大家还是拒绝服务攻击吧
leido
2019-01-30 01:37:29 +08:00
这是运算符重载,楼主偷换概念
Trumeet
2019-01-30 07:34:16 +08:00
数据类型 数据类型
master13
2019-01-30 08:18:20 +08:00
那按照楼主这逻辑,a+b=b+a 也不准确,当 a 和 b 都是 str 的时候……

玩这个有意思吗
Qzier
2019-01-30 08:20:18 +08:00
废话,+ 对应的是 __add__ 方法,+= 对应的是 __iadd__ 或 __iconcat__ 方法,当然不同,完全取决于你的实现!
Qzier
2019-01-30 08:23:36 +08:00
补充下 + 对应的方法还有 __concat__,是针对序列类型的
xiri
2019-01-30 08:33:13 +08:00
这是 python 里面特殊的运算符重载吧,一般说的+=是针对数值运算的。按楼主这样描述,我自己重定义一下,还可以实现其他功能呢
fan123199
2019-01-30 08:41:35 +08:00
介绍知识点不错,但不要搞个大新闻(常指标题浮夸),因为这个知识点很可能是常识。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/531710

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX