zoukankan      html  css  js  c++  java
  • 浅拷贝与深拷贝

    python的copy模块中分为copy和deepcopy函数;

    下面是python的官方解释:

    copy.copy(x)

    Return a shallow copy of x.

    copy.deepcopy(x)

    Return a deep copy of x.

    The difference between shallow and deep copying is only relevant for compound objects (objects that contain other objects, like lists or class instances):

    • A shallow copy constructs a new compound object and then (to the extent possible) inserts references into it to the objects found in the original.
    • A deep copy constructs a new compound object and then, recursively, inserts copies into it of the objects found in the original.

    Two problems often exist with deep copy operations that don’t exist with shallow copy operations:

    • Recursive objects (compound objects that, directly or indirectly, contain a reference to themselves) may cause a recursive loop.
    • Because deep copy copies everything it may copy too much, e.g., even administrative data structures that should be shared even between copies.

    The deepcopy() function avoids these problems by:

    • keeping a “memo” dictionary of objects already copied during the current copying pass; and
    • letting user-defined classes override the copying operation or the set of components copied.

    This module does not copy types like module, method, stack trace, stack frame, file, socket, window, array, or any similar types. It does “copy” functions and classes (shallow and deeply), by returning the original object unchanged; this is compatible with the way these are treated by the pickle module.

  • 相关阅读:
    C#读物
    那些健康手环真的值得买么?
    书籍推荐系列之一 -- 《凤凰项目:一个IT运维的传奇故事》
    测试
    HDU-2024 C语言合法标识符
    HDU-4548 美素数
    求最大流dinic算法模板
    最小费用最大流模板理解
    网络流初步——增广路代码的分析
    最短路的另外两种算法
  • 原文地址:https://www.cnblogs.com/testfan/p/8082753.html
Copyright © 2011-2022 走看看