Python 拷贝对象(深拷贝deepcopy与浅拷贝)

Python 拷贝对象(深拷贝deepcopy与浅拷贝)

>>> import copy
>>> a = [1,2,3,4,['a','v']]
>>> b = a
>>> b
[1, 2, 3, 4, ['a', 'v']]
>>> c = copy.copy(a)
>>> c
[1, 2, 3, 4, ['a', 'v']]
>>> d = copy.deepcopy(a)
>>> d
[1, 2, 3, 4, ['a', 'v']]
>>> a.append(5)
>>> a
[1, 2, 3, 4, ['a', 'v'], 5]
>>> b
[1, 2, 3, 4, ['a', 'v'], 5]
>>> c
[1, 2, 3, 4, ['a', 'v']]
>>> d
[1, 2, 3, 4, ['a', 'v']]
>>> a[4].append('c')
>>> a
[1, 2, 3, 4, ['a', 'v', 'c'], 5]
>>> b
[1, 2, 3, 4, ['a', 'v', 'c'], 5]
>>> c
[1, 2, 3, 4, ['a', 'v', 'c']]
>>> d
[1, 2, 3, 4, ['a', 'v']]

转载于:https://www.cnblogs.com/kungfupanda/archive/2011/10/01/2197055.html

你可能感兴趣的:(Python 拷贝对象(深拷贝deepcopy与浅拷贝))