+ 1
need a good explain for this code, pls :
arr=[1,2] a=arr print(a is not arr) >>> False # ?! it's like value different of var ? a=list(a) print(a) >>> [1,2] print(a is not arr) >>> True # ?! why ???
2 Answers
+ 1
In the first instance, a and arr are pointers to the same object (because of the line a=arr). So a is arr would return True. That is why a is not arr returns False.
For the second instance, a=list(a) means that a is now a copy of the list, not the same one. So here, a is arr will return False, so a is not arr returns True.
You can see this by looking at the id of a variable. a is arr is equivalent to id(a) == id(arr).
0
thk for answer