【Python】引用、浅拷贝、深拷贝
定义
可变对象常见类型: list,dict,set,bytearray (可变对象内存地址不会发生变化(id() 获取内存地址))
不可变对象常见类型:int,str,bool,float,None,tuple,bytes,frozenset
a = 3 # int 不可变对象 b = [1, 2] # list 可变对象 print("a = {}; b = {}".format(a, b)) print("改变前:a = {}, b = {}".format(id(a), id(b))) a = a+1 b.append(a) print("a = {}; b = {}".format(a, b)) print("改变后:a = {}, b = {}".format(id(a), id(b)))
执行结果:
引用、浅拷贝、深拷贝
引用:内存地址相同,子对象内存地址相同 (修改子对象 会 影响原数据)
浅拷贝:内存地址不同,可变子对象内存地址相同(修改可变子对象 会 影响原数据);不可变子对象内存地址不同(修改后不影响原数据)
深拷贝:内存地址不同,可变及不可变子对象内存地址不同(修改子对象数据 不会 影响原数据)
x = ["a", "b", "c", [1, 2, 3]] a = x b = copy.copy(x) c = copy.deepcopy(x) print("原始数据:x = {}, \n\t赋值给a = {} \n\t浅拷贝b = {} \n\t深拷贝c = {}".format(x, a, b, c)) print("内存地址:x = {}, \t赋值给a = {} \t浅拷贝b = {} \t深拷贝c = {}".format(id(x), id(a), id(b), id(c))) print("子对象内存地址:x = {}, \t赋值给a = {} \t浅拷贝b = {} \t深拷贝c = {}".format(id(x[3]), id(a[3]), id(b[3]), id(c[3]))) # x[3].append(4) b[3].append(4) c[3].append(88888) print("") print(">>> 原数据/浅拷贝 修改可变子对象:::影响原数据; 深拷贝修改可变子对象:::不影响原数据") print("原始数据:x = {}, \n\t赋值给a = {} \n\t浅拷贝b = {} \n\t深拷贝c = {}".format(x, a, b, c)) print("内存地址:x = {}, \t赋值给a = {} \t浅拷贝b = {} \t深拷贝c = {}".format(id(x), id(a), id(b), id(c))) print("子对象内存地址:x = {}, \t赋值给a = {} \t浅拷贝b = {} \t深拷贝c = {}".format(id(x[3]), id(a[3]), id(b[3]), id(c[3]))) b[2] = "cc" c[1] = "bb" print("") print(">>> 原数据/浅拷贝 修改不可变子对象;") print("原始数据:x = {}, \n\t赋值给a = {} \n\t浅拷贝b = {} \n\t深拷贝c = {}".format(x, a, b, c)) print("内存地址:x = {}, \t赋值给a = {} \t浅拷贝b = {} \t深拷贝c = {}".format(id(x), id(a), id(b), id(c))) print("子对象内存地址:x = {}, \t赋值给a = {} \t浅拷贝b = {} \t深拷贝c = {}".format(id(x[1]), id(a[1]), id(b[1]), id(c[1]))) print("子对象内存地址:x = {}, \t赋值给a = {} \t浅拷贝b = {} \t深拷贝c = {}".format(id(x[2]), id(a[2]), id(b[2]), id(c[2])))
执行结果:
x = ["a", "b", "c", [1, 2, 3]] a = x b = copy.copy(x) c = copy.deepcopy(x) print("原始数据:x = {}, \n\t赋值给a = {} \n\t浅拷贝b = {} \n\t深拷贝c = {}".format(x, a, b, c)) print("内存地址:x = {}, \t赋值给a = {} \t浅拷贝b = {} \t深拷贝c = {}".format(id(x), id(a), id(b), id(c))) print("子对象内存地址:x = {}, \t赋值给a = {} \t浅拷贝b = {} \t深拷贝c = {}".format(id(x[3]), id(a[3]), id(b[3]), id(c[3]))) # x[3].append(4) a.append("a") b[3].append(4) c[3].append(88888) print("") print(">>> 原数据/浅拷贝 修改可变子对象:::影响原数据; 深拷贝修改可变子对象:::不影响原数据") print("原始数据:x = {}, \n\t赋值给a = {} \n\t浅拷贝b = {} \n\t深拷贝c = {}".format(x, a, b, c)) print("内存地址:x = {}, \t赋值给a = {} \t浅拷贝b = {} \t深拷贝c = {}".format(id(x), id(a), id(b), id(c))) print("子对象内存地址:x = {}, \t赋值给a = {} \t浅拷贝b = {} \t深拷贝c = {}".format(id(x[3]), id(a[3]), id(b[3]), id(c[3]))) a[0] = "aa" b[2] = "cc" c[1] = "bb" print("") print(">>> 原数据/浅拷贝 修改不可变子对象;") print("原始数据:x = {}, \n\t赋值给a = {} \n\t浅拷贝b = {} \n\t深拷贝c = {}".format(x, a, b, c)) print("内存地址:x = {}, \t赋值给a = {} \t浅拷贝b = {} \t深拷贝c = {}".format(id(x), id(a), id(b), id(c))) print("子对象内存地址:x = {}, \t赋值给a = {} \t浅拷贝b = {} \t深拷贝c = {}".format(id(x[1]), id(a[1]), id(b[1]), id(c[1]))) print("子对象内存地址:x = {}, \t赋值给a = {} \t浅拷贝b = {} \t深拷贝c = {}".format(id(x[2]), id(a[2]), id(b[2]), id(c[2])))
原始数据:x = ['a', 'b', 'c', [1, 2, 3]], 赋值给a = ['a', 'b', 'c', [1, 2, 3]] 浅拷贝b = ['a', 'b', 'c', [1, 2, 3]] 深拷贝c = ['a', 'b', 'c', [1, 2, 3]] 内存地址:x = 81173896, 赋值给a = 81173896 浅拷贝b = 81202504 深拷贝c = 81202440 子对象内存地址:x = 81202696, 赋值给a = 81202696 浅拷贝b = 81202696 深拷贝c = 81202088 >>> 原数据/浅拷贝 修改可变子对象:::影响原数据; 深拷贝修改可变子对象:::不影响原数据 原始数据:x = ['a', 'b', 'c', [1, 2, 3, 4], 'a'], 赋值给a = ['a', 'b', 'c', [1, 2, 3, 4], 'a'] 浅拷贝b = ['a', 'b', 'c', [1, 2, 3, 4]] 深拷贝c = ['a', 'b', 'c', [1, 2, 3, 88888]] 内存地址:x = 81173896, 赋值给a = 81173896 浅拷贝b = 81202504 深拷贝c = 81202440 子对象内存地址:x = 81202696, 赋值给a = 81202696 浅拷贝b = 81202696 深拷贝c = 81202088 >>> 原数据/浅拷贝 修改不可变子对象; 原始数据:x = ['aa', 'b', 'c', [1, 2, 3, 4], 'a'], 赋值给a = ['aa', 'b', 'c', [1, 2, 3, 4], 'a'] 浅拷贝b = ['a', 'b', 'cc', [1, 2, 3, 4]] 深拷贝c = ['a', 'bb', 'c', [1, 2, 3, 88888]] 内存地址:x = 81173896, 赋值给a = 81173896 浅拷贝b = 81202504 深拷贝c = 81202440 子对象内存地址:x = 45594528, 赋值给a = 45594528 浅拷贝b = 45594528 深拷贝c = 81548032 子对象内存地址:x = 45504832, 赋值给a = 45504832 浅拷贝b = 81548000 深拷贝c = 45504832
-------------------------------------------------------------------------------------
如果万事开头难 那请结局一定圆满 @ Phoenixy
-------------------------------------------------------------------------------------