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实现。

总结

以上是 Python中表达式x += y和x = x+y 的区别详解 的全部内容, 来源链接: utcz.com/z/356086.html

回到顶部