The expression x += y and x = x+y

  • 2020-06-07 04:41:39
  • OfStack

preface

This article mainly introduces the difference between x += y and x = x+y in Python. Share it for your reference and study. Here is a detailed introduction:

Look directly at the following code:

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

You can see that for mutable objects the form x = x + y has changed the point of x instead of "inplace".

dis module was used for 1 verification.


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

You can see that the bytecode is different:

Corresponding INPLACE_ADD + = Corresponding BINARY_ADD

Note: The above applies only to the CPython implementation.

conclusion


Related articles: