-1

I am sorry if this question is replied somewhere since python (2.7) is a huge topic here, but I am not sure how to even look for it. I am having this code.

>>> v=[1,2,3]
>>> f=v
>>> v+=[]
>>> f is v
True
>>> v=v+[]
>>> f is v
False

Could explain to me why += operator is different from v=v+? Why does the first one doesn't create a new object in memory whereas another does although they are bound to be equivalent?

AVX
  • 319
  • 1
  • 3
  • 13

1 Answers1

1

By v=v+[], you are assigning a new list v+[] to v. Check out its id:

>>> v = [1,2,3]
>>> f=v
>>> id(v)
35713992L
>>> v+=[]
>>> id(v)
35713992L
>>> v=v+[]
>>> id(v)
35692232L
Yu Hao
  • 119,891
  • 44
  • 235
  • 294