首页 > 学院 > 开发设计 > 正文

Python(2.7.6)copy-浅拷贝与深拷贝

2019-11-14 17:20:48
字体:
来源:转载
供稿:网友

Python 标准库的 copy 模块提供了对象拷贝的功能。 copy 模块中有两个函数 copy 和 deepcopy,分别支持浅拷贝与深拷贝。

copy_demo.py

import copyclass MyClass(object):    def __init__(self, name):        super(MyClass, self).__init__()        self.name = namea = [MyClass('huey')]b = copy.copy(a)c = copy.deepcopy(a)PRint 'a is b?', a is b                # a is b? False        print 'a == b?', a == b                # a == b? Trueprint 'a is c?', a is c                # a is c? Falseprint 'a == c?', a == c                # a == c? Falsea[0].name = 'sugar'print 'a[0].name =', a[0].name        # a[0].name = sugarprint 'b[0].name =', b[0].name        # b[0].name = sugarprint 'c[0].name =', c[0].name        # c[0].name = huey

 


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表