前言
本文主要给大家介绍的是关于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
<