美文网首页
python浅拷贝和深拷贝

python浅拷贝和深拷贝

作者: 晨语凡心 | 来源:发表于2019-02-17 17:01 被阅读0次

    Variable Assignment

    n = 300

    Python variable n is a symbolic name that is a reference or pointer to an object.

    m = n

    Python does not create another object. It simply creates a new symbolic name or reference, m, which points to the same object that n points to.

    m=400

    Now Python creates a new integer object with the value 400, and m becomes a reference to it.

    Object Identity

    In Python, every object that is created is given a number that uniquely identifies it. It is guaranteed that no two objects will have the same identifier during any period in which their lifetimes overlap.

    The built-in Python function id() returns an object’s integer identifier.

    >>> m=30

    >>> n=30

    >>> id(m)

    1405569120

    >>> id(n)

    1405569120

    For purposes of optimization, the interpreter creates objects for the integers in the range [-5, 256] at startup, and then reuses them during program execution. Thus, when you assign separate variables to an integer value in this range, they will actually reference the same object.

    Make shallow copies on built-in mutable collections: list, dict, set

    new_list=list(original_list)

    new_dict=dict(original_dict)

    new_set=set(original_set)

    this method won’t work for custom objects and, on top of that, it only creates shallow copies.

    >>> xs=[[1,2,3],[4,5,6],[7,8,9]]

    >>> ys=list(xs) # Make a shallow copy

    Add a new sublist to the original (xs) and this modification won’t affect the copy (ys)

    >>> xs.append(['new sublist'])

    >>> xs[[1, 2, 3], [4, 5, 6], [7, 8, 9], ['new sublist']]

    >>> ys[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

    When you modify one of the child objects in xs, this modification will be reflected in ys as well—that’s because we only created a shallow copy of the original list, ys still contains references to the original child objects stored in xs. These children were not copied. They were merely referenced again in the copied list.

    >>> xs[1][0]='X'

    >>> xs[[1, 2, 3], ['X', 5, 6], [7, 8, 9], ['new sublist']]

    >>> ys[[1, 2, 3], ['X', 5, 6], [7, 8, 9]]

    We can use copy() function defined in copy module to make shallow  copies on custom or built-in objects.

    Make deep copies on custom or built-in objects

    >>> import  copy

    >>> xs=[[1,2,3],[4,5,6],[7,8,9]]

    >>> zs=copy.deepcopy(xs)

    The copy list zs now is fully independent of the original list xs.

    To sum up

    1: Making a shallow copy of an object won’t clone child objects. Therefore, the copy is not fully independent of the original.

    2: A deep copy of an object will recursively clone child objects. The clone is fully independent of the original, but creating a deep copy is slower.

    3: You can copy arbitrary objects (including custom classes) with the copy module.

    相关文章

      网友评论

          本文标题:python浅拷贝和深拷贝

          本文链接:https://www.haomeiwen.com/subject/oovgeqtx.html