copy
— Shallow and deep copy operations¶
Source code:Lib/copy.py
Assignment statements in Python do not copy objects, they create bindingsbetween a target and an object. For collections that are mutable or containmutable items, a copy is sometimes needed so one can change one copy withoutchanging the other. This module provides generic shallow and deep copyoperations (explained below).
Interface summary:
- copy.copy(obj)¶
Return a shallow copy ofobj.
- copy.deepcopy(obj[,memo])¶
Return a deep copy ofobj.
- copy.replace(obj,/,**changes)¶
Creates a new object of the same type asobj, replacing fields with valuesfromchanges.
Added in version 3.13.
- exceptioncopy.Error¶
Raised for module specific errors.
The difference between shallow and deep copying is only relevant for compoundobjects (objects that contain other objects, like lists or class instances):
Ashallow copy constructs a new compound object and then (to the extentpossible) insertsreferences into it to the objects found in the original.
Adeep copy constructs a new compound object and then, recursively, insertscopies into it of the objects found in the original.
Two problems often exist with deep copy operations that don’t exist with shallowcopy operations:
Recursive objects (compound objects that, directly or indirectly, contain areference to themselves) may cause a recursive loop.
Because deep copy copies everything it may copy too much, such as datawhich is intended to be shared between copies.
Thedeepcopy()
function avoids these problems by:
keeping a
memo
dictionary of objects already copied during the currentcopying pass; andletting user-defined classes override the copying operation or the set ofcomponents copied.
This module does not copy types like module, method, stack trace, stack frame,file, socket, window, or any similar types. It does «copy» functions andclasses (shallow and deeply), by returning the original object unchanged; thisis compatible with the way these are treated by thepickle
module.
Shallow copies of dictionaries can be made usingdict.copy()
, andof lists by assigning a slice of the entire list, for example,copied_list=original_list[:]
.
Classes can use the same interfaces to control copying that they use to controlpickling. See the description of modulepickle
for information on thesemethods. In fact, thecopy
module uses the registeredpickle functions from thecopyreg
module.
In order for a class to define its own copy implementation, it can definespecial methods__copy__()
and__deepcopy__()
.
- object.__copy__(self)¶
Called to implement the shallow copy operation;no additional arguments are passed.
- object.__deepcopy__(self,memo)¶
Called to implement the deep copy operation; it is passed oneargument, thememo dictionary. If the
__deepcopy__
implementation needsto make a deep copy of a component, it should call thedeepcopy()
functionwith the component as first argument and thememo dictionary as second argument.Thememo dictionary should be treated as an opaque object.
Functioncopy.replace()
is more limitedthancopy()
anddeepcopy()
,and only supports named tuples created bynamedtuple()
,dataclasses
, and other classes which define method__replace__()
.
- object.__replace__(self,/,**changes)¶
This method should create a new object of the same type,replacing fields with values fromchanges.
Δείτε επίσης
- Module
pickle
Discussion of the special methods used to support object state retrieval andrestoration.