[Learning Python] shallow vs. deep copy

import copy
l=[1,3.14,[2,4,6]]
l2=copy.copy(l)
print(l)
print(l2)

print(id(l))
print(id(l2))

print('------------------------')

for i in l:
    print(id(i))
print('------------------------')

for i in l2:
    print(id(i))
Although visible # l and l2 address is not the same, but in which the contents are the same
#copy i.e. corresponding to two different pointer addresses shallow copy, l and l2 is not the same
'''
Shallow copy features:
After two shallow copy memory address sequences are not identical
Two sequences, the same elements in the same memory address index

'''
print('------------------------')
print('------------------------')
print('------------------------')

l3=copy.deepcopy(l)

print('------------------------')

for i in l3:
    print(id(i))
# Visible after a deep copy significantly different address

Guess you like

Origin www.cnblogs.com/cyber-shady/p/11520450.html