Created
June 17, 2019 00:23
-
-
Save gatheluck/8ee1b4420414bc903423c2024aceaee0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This code is for self study. | |
# Remider of the difference between copy.copy and copy.deepcopy | |
# immutable objects | |
print('immutable object') | |
a = 'abc' | |
b = a | |
print("a:{}, id(a):{}".format(a, id(a))) | |
print("b:{}, id(b):{}".format(b, id(b))) | |
print('resubstituting...') | |
a = 'abcd' | |
print("a:{}, id(a):{}".format(a, id(a))) | |
print("b:{}, id(b):{}".format(b, id(b))) | |
# mutable objects | |
print('mutable object') | |
a = list('a b c'.split(' ')) | |
b = a | |
print("a:{}, id(a):{}".format(a, id(a))) | |
print("b:{}, id(b):{}".format(b, id(b))) | |
print('resubstituting...') | |
a.append('d') | |
print("a:{}, id(a):{}".format(a, id(a))) | |
print("b:{}, id(b):{}".format(b, id(b))) | |
import copy | |
print('check copy and deep copy') | |
a = [['a'], ['b']] | |
b = a | |
c = copy.copy(a) | |
d = copy.deepcopy(a) | |
e = a[:] | |
print("a:{}, id(a):{}".format(a, id(a))) | |
print("b:{}, id(b):{}".format(b, id(b))) | |
print("c:{}, id(c):{}".format(c, id(c))) | |
print("d:{}, id(d):{}".format(d, id(d))) | |
print("e:{}, id(e):{}".format(e, id(e))) | |
print('resubstituting...') | |
a[0].append('c') | |
print("a:{}, id(a):{}".format(a, id(a))) | |
print("b:{}, id(b):{}".format(b, id(b))) | |
print("c:{}, id(c):{}".format(c, id(c))) | |
print("d:{}, id(d):{}".format(d, id(d))) | |
print("e:{}, id(e):{}".format(e, id(e))) | |
print('resubstituting...') | |
a.append('d') | |
print("a:{}, id(a):{}".format(a, id(a))) | |
print("b:{}, id(b):{}".format(b, id(b))) | |
print("c:{}, id(c):{}".format(c, id(c))) | |
print("d:{}, id(d):{}".format(d, id(d))) | |
print("e:{}, id(e):{}".format(e, id(e))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment