1- # Generic (shallow and deep) copying operations
2- # =============================================
3- #
4- # The difference between shallow and deep copying is only relevant for
5- # compound objects (objects that contain other objects, like lists or class
6- # instances).
7- #
8- # - A shallow copy constructs a new compound object and then (to the extent
9- # possible) inserts *the same objects* into in that the original contains.
10- #
11- # - A deep copy constructs a new compound object and then, recursively,
12- # inserts *copies* into it of the objects found in the original.
13- #
14- # Two problems often exist with deep copy operations that don't exist with
15- # shallow copy operations:
16- #
17- # (a) recursive objects (compound objects that, directly or indirectly,
18- # contain a reference to themselves) may cause a recursive loop
19- #
20- # (b) because deep copy copies *everything* it may copy too much, e.g.
21- # administrative data structures that should be shared even between copies
22- #
23- # Python's deep copy operation avoids these problems by:
24- #
25- # (a) keeping a table of objects already copied during the current copying pass
26- #
27- # (b) letting user-defined classes override the copying operation or the set
28- # of components copied
29- #
30- # This version does not copy types like module, class, function, method,
31- # nor stack trace, stack frame, nor file, socket, window, nor array,
32- # nor any similar types.
1+ """\
2+ Generic (shallow and deep) copying operations
3+ =============================================
334
5+ Interface summary:
6+
7+ import copy
8+
9+ x = copy.copy(y) # make a shallow copy of y
10+ x = copy.deepcopy(y) # make a deep copy of y
11+
12+ For module specific errors, copy.Error is raised.
13+
14+ The difference between shallow and deep copying is only relevant for
15+ compound objects (objects that contain other objects, like lists or
16+ class instances).
17+
18+ - A shallow copy constructs a new compound object and then (to the
19+ extent possible) inserts *the same objects* into in that the
20+ original contains.
21+
22+ - A deep copy constructs a new compound object and then, recursively,
23+ inserts *copies* into it of the objects found in the original.
24+
25+ Two problems often exist with deep copy operations that don't exist
26+ with shallow copy operations:
27+
28+ (a) recursive objects (compound objects that, directly or indirectly,
29+ contain a reference to themselves) may cause a recursive loop
30+
31+ (b) because deep copy copies *everything* it may copy too much, e.g.
32+ administrative data structures that should be shared even between
33+ copies
34+
35+ Python's deep copy operation avoids these problems by:
36+
37+ (a) keeping a table of objects already copied during the current
38+ copying pass
39+
40+ (b) letting user-defined classes override the copying operation or the
41+ set of components copied
42+
43+ This version does not copy types like module, class, function, method,
44+ nor stack trace, stack frame, nor file, socket, window, nor array, nor
45+ any similar types.
46+
47+ Classes can use the same interfaces to control copying that they use
48+ to control pickling: they can define methods called __getinitargs__(),
49+ __getstate__() and __setstate__(). See the __doc__ string of module
50+ "pickle" for information on these methods.
51+ """
3452
3553import types
3654
3755Error = 'copy.Error'
3856
3957def copy (x ):
58+ """Shallow copy operation on arbitrary Python objects.
59+
60+ See the module's __doc__ string for more info.
61+ """
62+
4063 try :
4164 copierfunction = _copy_dispatch [type (x )]
4265 except KeyError :
4366 try :
4467 copier = x .__copy__
4568 except AttributeError :
46- raise Error , "un(shallow)copyable object of type %s" % type (x )
69+ raise Error , \
70+ "un(shallow)copyable object of type %s" % type (x )
4771 y = copier ()
4872 else :
4973 y = copierfunction (x )
@@ -100,6 +124,11 @@ def _copy_inst(x):
100124del d
101125
102126def deepcopy (x , memo = None ):
127+ """Deep copy operation on arbitrary Python objects.
128+
129+ See the module's __doc__ string for more info.
130+ """
131+
103132 if memo is None :
104133 memo = {}
105134 d = id (x )
@@ -111,7 +140,8 @@ def deepcopy(x, memo = None):
111140 try :
112141 copier = x .__deepcopy__
113142 except AttributeError :
114- raise Error , "un-deep-copyable object of type %s" % type (x )
143+ raise Error , \
144+ "un-deep-copyable object of type %s" % type (x )
115145 y = copier (memo )
116146 else :
117147 y = copierfunction (x , memo )
0 commit comments