17db96d56Sopenharmony_ci:mod:`copy` --- Shallow and deep copy operations 27db96d56Sopenharmony_ci================================================ 37db96d56Sopenharmony_ci 47db96d56Sopenharmony_ci.. module:: copy 57db96d56Sopenharmony_ci :synopsis: Shallow and deep copy operations. 67db96d56Sopenharmony_ci 77db96d56Sopenharmony_ci**Source code:** :source:`Lib/copy.py` 87db96d56Sopenharmony_ci 97db96d56Sopenharmony_ci-------------- 107db96d56Sopenharmony_ci 117db96d56Sopenharmony_ciAssignment statements in Python do not copy objects, they create bindings 127db96d56Sopenharmony_cibetween a target and an object. For collections that are mutable or contain 137db96d56Sopenharmony_cimutable items, a copy is sometimes needed so one can change one copy without 147db96d56Sopenharmony_cichanging the other. This module provides generic shallow and deep copy 157db96d56Sopenharmony_cioperations (explained below). 167db96d56Sopenharmony_ci 177db96d56Sopenharmony_ci 187db96d56Sopenharmony_ciInterface summary: 197db96d56Sopenharmony_ci 207db96d56Sopenharmony_ci.. function:: copy(x) 217db96d56Sopenharmony_ci 227db96d56Sopenharmony_ci Return a shallow copy of *x*. 237db96d56Sopenharmony_ci 247db96d56Sopenharmony_ci 257db96d56Sopenharmony_ci.. function:: deepcopy(x[, memo]) 267db96d56Sopenharmony_ci 277db96d56Sopenharmony_ci Return a deep copy of *x*. 287db96d56Sopenharmony_ci 297db96d56Sopenharmony_ci 307db96d56Sopenharmony_ci.. exception:: Error 317db96d56Sopenharmony_ci 327db96d56Sopenharmony_ci Raised for module specific errors. 337db96d56Sopenharmony_ci 347db96d56Sopenharmony_ci.. _shallow_vs_deep_copy: 357db96d56Sopenharmony_ci 367db96d56Sopenharmony_ciThe difference between shallow and deep copying is only relevant for compound 377db96d56Sopenharmony_ciobjects (objects that contain other objects, like lists or class instances): 387db96d56Sopenharmony_ci 397db96d56Sopenharmony_ci* A *shallow copy* constructs a new compound object and then (to the extent 407db96d56Sopenharmony_ci possible) inserts *references* into it to the objects found in the original. 417db96d56Sopenharmony_ci 427db96d56Sopenharmony_ci* A *deep copy* constructs a new compound object and then, recursively, inserts 437db96d56Sopenharmony_ci *copies* into it of the objects found in the original. 447db96d56Sopenharmony_ci 457db96d56Sopenharmony_ciTwo problems often exist with deep copy operations that don't exist with shallow 467db96d56Sopenharmony_cicopy operations: 477db96d56Sopenharmony_ci 487db96d56Sopenharmony_ci* Recursive objects (compound objects that, directly or indirectly, contain a 497db96d56Sopenharmony_ci reference to themselves) may cause a recursive loop. 507db96d56Sopenharmony_ci 517db96d56Sopenharmony_ci* Because deep copy copies everything it may copy too much, such as data 527db96d56Sopenharmony_ci which is intended to be shared between copies. 537db96d56Sopenharmony_ci 547db96d56Sopenharmony_ciThe :func:`deepcopy` function avoids these problems by: 557db96d56Sopenharmony_ci 567db96d56Sopenharmony_ci* keeping a ``memo`` dictionary of objects already copied during the current 577db96d56Sopenharmony_ci copying pass; and 587db96d56Sopenharmony_ci 597db96d56Sopenharmony_ci* letting user-defined classes override the copying operation or the set of 607db96d56Sopenharmony_ci components copied. 617db96d56Sopenharmony_ci 627db96d56Sopenharmony_ciThis module does not copy types like module, method, stack trace, stack frame, 637db96d56Sopenharmony_cifile, socket, window, or any similar types. It does "copy" functions and 647db96d56Sopenharmony_ciclasses (shallow and deeply), by returning the original object unchanged; this 657db96d56Sopenharmony_ciis compatible with the way these are treated by the :mod:`pickle` module. 667db96d56Sopenharmony_ci 677db96d56Sopenharmony_ciShallow copies of dictionaries can be made using :meth:`dict.copy`, and 687db96d56Sopenharmony_ciof lists by assigning a slice of the entire list, for example, 697db96d56Sopenharmony_ci``copied_list = original_list[:]``. 707db96d56Sopenharmony_ci 717db96d56Sopenharmony_ci.. index:: pair: module; pickle 727db96d56Sopenharmony_ci 737db96d56Sopenharmony_ciClasses can use the same interfaces to control copying that they use to control 747db96d56Sopenharmony_cipickling. See the description of module :mod:`pickle` for information on these 757db96d56Sopenharmony_cimethods. In fact, the :mod:`copy` module uses the registered 767db96d56Sopenharmony_cipickle functions from the :mod:`copyreg` module. 777db96d56Sopenharmony_ci 787db96d56Sopenharmony_ci.. index:: 797db96d56Sopenharmony_ci single: __copy__() (copy protocol) 807db96d56Sopenharmony_ci single: __deepcopy__() (copy protocol) 817db96d56Sopenharmony_ci 827db96d56Sopenharmony_ciIn order for a class to define its own copy implementation, it can define 837db96d56Sopenharmony_cispecial methods :meth:`__copy__` and :meth:`__deepcopy__`. The former is called 847db96d56Sopenharmony_cito implement the shallow copy operation; no additional arguments are passed. 857db96d56Sopenharmony_ciThe latter is called to implement the deep copy operation; it is passed one 867db96d56Sopenharmony_ciargument, the ``memo`` dictionary. If the :meth:`__deepcopy__` implementation needs 877db96d56Sopenharmony_cito make a deep copy of a component, it should call the :func:`deepcopy` function 887db96d56Sopenharmony_ciwith the component as first argument and the memo dictionary as second argument. 897db96d56Sopenharmony_ciThe memo dictionary should be treated as an opaque object. 907db96d56Sopenharmony_ci 917db96d56Sopenharmony_ci 927db96d56Sopenharmony_ci.. seealso:: 937db96d56Sopenharmony_ci 947db96d56Sopenharmony_ci Module :mod:`pickle` 957db96d56Sopenharmony_ci Discussion of the special methods used to support object state retrieval and 967db96d56Sopenharmony_ci restoration. 977db96d56Sopenharmony_ci 98