need a good explain for this code, pls : | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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 ???

8th Jun 2020, 10:42 AM
Oneill~Онеилл~奥尼尔~ओनील~اونیل~*‎
Oneill~Онеилл~奥尼尔~ओनील~اونیل~*‎ - avatar
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).
8th Jun 2020, 12:41 PM
Russ
Russ - avatar
0
thk for answer
10th Jun 2020, 9:16 AM
Oneill~Онеилл~奥尼尔~ओनील~اونیل~*‎
Oneill~Онеилл~奥尼尔~ओनील~اونیل~*‎ - avatar