0
T1 = (1,[5],3)
T2 = T1
print("T2 :",T2)
T1[1].append(6)
print("T1 :",T1)
print("T2 :",T2)

result:
T2 : (1, [5], 3)
T1 : (1, [5, 6], 3)
T2 : (1, [5, 6], 3)

when T1 append new value, why T2 equals to T1? I think T1 = (1,[5,6],3) and T2 = (1, [5], 3), but finally T2 is (1,[5,6],3)

Til
  • 5,150
  • 13
  • 26
  • 34
  • 3
    Because, they both are same object. You need to copy one to other: `T2 = copy.deepcopy(T1)`. – Austin Mar 10 '19 at 04:03
  • this answer may help [https://stackoverflow.com/questions/2612802/](https://stackoverflow.com/questions/2612802/) – jia Jimmy Mar 10 '19 at 04:10

1 Answers1

0

The line T2 = T1 has the effect of binding the name T2 to the same object as what T1 is already bound to.

(It isn't creating a copy of the object)

This is true for any assignment statement in Python of the from <LeftName> = <RightName>.

This means, any modification done by your code, using the name T1, can be seen using the name T2, and vice versa.

fountainhead
  • 3,584
  • 1
  • 8
  • 17