+ 2
How copying of lists work in Python?
>>>a=[2,3,4] >>>b=a >>> # now a and b, both refers same memory address >>># check memory addresses with print(hex(id(a))) and print(hex(id(b))) >>>del a >>>b [2,3,4] So where does this variable [2,3,4] actually stored? Explain, please.
3 Antworten
+ 8
The list is stored until at least one variable points to it. The del command just destroys one of the pointers. The list itself is stored in yet another place - try to print hex(id([2,3,4])) along with those of a and b, to see that.
+ 1
notable point of list copying is list by default follows shallow copy.
here is code you can check and understand.
https://code.sololearn.com/cDssM174GLKf/?ref=app
0
Actually python has automatic memory management.
=> Automatic memory management: If the values of python variables are same then it allocates a single memory block.
so if you have delete one obj , other is still pointing to same memory location.