Python中表达式x += y和x = x+y 的区别详解
前言
本文主要给大家介绍的是关于Python中表达式x += y和x = x+y 区别的相关内容,分享出来供大家参考学习,下面来看看详细的介绍:
直接看下面代码:
x +=y
In [66]: id(a) Out[66]: 4476839480 In [67]: id(b) Out[67]: 4476839480 In [68]: a = [1, 2, 3] In [69]: b = a In [70]: id(a) Out[70]: 4477149984 In [71]: id(b) Out[71]: 4477149984 In [72]: a += [4, 5, 6] In [73]: a Out[73]: [1, 2, 3, 4, 5, 6] In [74]: b Out[74]: [1, 2, 3, 4, 5, 6] In [75]: id(a) Out[75]: 4477149984 In [76]: id(b) Out[76]: 4477149984
x = x + y
In [77]: a = [1, 2, 3] In [78]: b = a In [79]: id(a) Out[79]: 4476686208 In [80]: id(b) Out[80]: 4476686208 In [81]: a = a + [4, 5, 6] In [82]: a Out[82]: [1, 2, 3, 4, 5, 6] In [83]: b Out[83]: [1, 2, 3] In [84]: id(a) Out[84]: 4477187640 In [85]: id(b) Out[85]: 4476686208
可以看出对于可变对象而言 x = x + y 这种形式已经改变了x的指向,而不是"inplace"的修改。
使用dis模块进行一下验证。
In [86]: def foo(x, y): ...: x += y ...: return x ...: In [87]: def foo(x, y): ...: x += y ...: In [88]: def foo2(x, y): ...: x = x + y ...: In [89]: dis.dis(foo) 2 0 LOAD_FAST 0 (x) 3 LOAD_FAST 1 (y) 6 INPLACE_ADD 7 STORE_FAST 0 (x) 10 LOAD_CONST 0 (None) 13 RETURN_VALUE In [90]: dis.dis(foo2) 2 0 LOAD_FAST 0 (x) 3 LOAD_FAST 1 (y) 6 BINARY_ADD 7 STORE_FAST 0 (x) 10 LOAD_CONST 0 (None) 13 RETURN_VALUE
可以看到字节码是不同的:
- += 对应 INPLACE_ADD
- 对应BINARY_ADD
注意:以上仅适用于CPython实现。
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对安科网的支持。
相关推荐
YENCSDN 2020-11-17
lsjweiyi 2020-11-17
houmenghu 2020-11-17
Erick 2020-11-17
HeyShHeyou 2020-11-17
以梦为马不负韶华 2020-10-20
lhtzbj 2020-11-17
夜斗不是神 2020-11-17
pythonjw 2020-11-17
dingwun 2020-11-16
lhxxhl 2020-11-16
坚持是一种品质 2020-11-16
染血白衣 2020-11-16
huavhuahua 2020-11-20
meylovezn 2020-11-20
逍遥友 2020-11-20
weiiron 2020-11-16