+ 2
Python (for me) odd behavior with variable copy. Explanations?
I had a code not working properly. After finding the problem I wrote a little code to demonstrate it. Can anyone explain this odd behavior of the copy when using .pop? https://code.sololearn.com/cZH4x4Zj99pd/?ref=app
2 Respostas
+ 9
You're working with the same object.
sol=[1,2,3,4,5]
solo=sol
# same id
print(id(sol), id(solo))
# Use the copy import, show documentation
import copy
print(copy.__doc__)
# new copy, new id
solo2 = copy.copy(solo)
print(id(solo2))
+ 1
Both of them refer to the same object.