copy-example.py

# -*- coding: utf-8 -*-

# @Date    : 2018-11-01
# @Author  : Peng Shiyu

import copy

# 浅拷贝shallow copy 复制对象本身,
# 容器container 对象的成员仍然指向原来成员对象

def shallow_copy():
    lst = [[0], [1], [2]]

    lst1 = copy.copy(lst)
    lst2 = lst[:]

    lst1[0].append(None)

    print(lst)
    print(lst1)
    print(lst2)

"""
[[0, None], [1], [2]]
[[0, None], [1], [2]]
[[0, None], [1], [2]]
"""

# 深拷贝deep copy
# 容器container 对象的所有成员被递归复制

def deep_copy():
    lst = [[0], [1], [2]]

    lst1 = copy.deepcopy(lst)

    lst1[0].append(None)

    print(lst)
    print(lst1)

"""
[[0], [1], [2]]
[[0, None], [1], [2]]
"""