Home > Backend Development > Python Tutorial > Python 拷贝对象(深拷贝deepcopy与浅拷贝copy)

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

WBOY
Release: 2016-06-16 08:47:32
Original
1127 people have browsed it

1. copy.copy 浅拷贝 只拷贝父对象,不会拷贝对象的内部的子对象。
2. copy.deepcopy 深拷贝 拷贝对象及其子对象
一个很好的例子:

import copy
= [1234, ['a''b']]  #原始对象

= a  #赋值,传对象的引用
= copy.copy(a)  #对象拷贝,浅拷贝
= copy.deepcopy(a)  #对象拷贝,深拷贝

a.append(
5)  #修改对象a
a[4].append('c')  #修改对象a中的['a', 'b']数组对象

print 'a = ', a
print 'b = ', b
print 'c = ', c
print 'd = ', d

输出结果:
a =  [1, 2, 3, 4, ['a', 'b', 'c'], 5]
b =  [1, 2, 3, 4, ['a', 'b', 'c'], 5]
c =  [1, 2, 3, 4, ['a', 'b', 'c']]
d =  [1, 2, 3, 4, ['a', 'b']]
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template