Guido van Rossum | 4b8c6ea | 2000-02-04 15:39:30 +0000 | [diff] [blame] | 1 | """Generic (shallow and deep) copying operations. |
Guido van Rossum | 409780f | 1995-01-10 00:34:21 +0000 | [diff] [blame] | 2 | |
Guido van Rossum | cc6764c | 1995-02-09 17:18:10 +0000 | [diff] [blame] | 3 | Interface summary: |
| 4 | |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 5 | import copy |
Guido van Rossum | cc6764c | 1995-02-09 17:18:10 +0000 | [diff] [blame] | 6 | |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 7 | x = copy.copy(y) # make a shallow copy of y |
| 8 | x = copy.deepcopy(y) # make a deep copy of y |
Guido van Rossum | cc6764c | 1995-02-09 17:18:10 +0000 | [diff] [blame] | 9 | |
Guido van Rossum | 55d2f39 | 1995-03-14 17:41:36 +0000 | [diff] [blame] | 10 | For module specific errors, copy.error is raised. |
Guido van Rossum | cc6764c | 1995-02-09 17:18:10 +0000 | [diff] [blame] | 11 | |
| 12 | The difference between shallow and deep copying is only relevant for |
| 13 | compound objects (objects that contain other objects, like lists or |
| 14 | class instances). |
| 15 | |
| 16 | - A shallow copy constructs a new compound object and then (to the |
| 17 | extent possible) inserts *the same objects* into in that the |
| 18 | original contains. |
| 19 | |
| 20 | - A deep copy constructs a new compound object and then, recursively, |
| 21 | inserts *copies* into it of the objects found in the original. |
| 22 | |
| 23 | Two problems often exist with deep copy operations that don't exist |
| 24 | with shallow copy operations: |
| 25 | |
Guido van Rossum | f7cea10 | 1997-05-28 19:31:14 +0000 | [diff] [blame] | 26 | a) recursive objects (compound objects that, directly or indirectly, |
Guido van Rossum | cc6764c | 1995-02-09 17:18:10 +0000 | [diff] [blame] | 27 | contain a reference to themselves) may cause a recursive loop |
| 28 | |
Guido van Rossum | f7cea10 | 1997-05-28 19:31:14 +0000 | [diff] [blame] | 29 | b) because deep copy copies *everything* it may copy too much, e.g. |
Guido van Rossum | cc6764c | 1995-02-09 17:18:10 +0000 | [diff] [blame] | 30 | administrative data structures that should be shared even between |
| 31 | copies |
| 32 | |
| 33 | Python's deep copy operation avoids these problems by: |
| 34 | |
Guido van Rossum | f7cea10 | 1997-05-28 19:31:14 +0000 | [diff] [blame] | 35 | a) keeping a table of objects already copied during the current |
| 36 | copying pass |
Guido van Rossum | cc6764c | 1995-02-09 17:18:10 +0000 | [diff] [blame] | 37 | |
Guido van Rossum | f7cea10 | 1997-05-28 19:31:14 +0000 | [diff] [blame] | 38 | b) letting user-defined classes override the copying operation or the |
Guido van Rossum | cc6764c | 1995-02-09 17:18:10 +0000 | [diff] [blame] | 39 | set of components copied |
| 40 | |
| 41 | This version does not copy types like module, class, function, method, |
| 42 | nor stack trace, stack frame, nor file, socket, window, nor array, nor |
| 43 | any similar types. |
| 44 | |
| 45 | Classes can use the same interfaces to control copying that they use |
| 46 | to control pickling: they can define methods called __getinitargs__(), |
Guido van Rossum | c5d2d51 | 1997-12-07 16:18:22 +0000 | [diff] [blame] | 47 | __getstate__() and __setstate__(). See the documentation for module |
Guido van Rossum | cc6764c | 1995-02-09 17:18:10 +0000 | [diff] [blame] | 48 | "pickle" for information on these methods. |
| 49 | """ |
Guido van Rossum | 409780f | 1995-01-10 00:34:21 +0000 | [diff] [blame] | 50 | |
Guido van Rossum | abfdd70 | 1997-10-07 14:47:50 +0000 | [diff] [blame] | 51 | # XXX need to support copy_reg here too... |
| 52 | |
Guido van Rossum | 409780f | 1995-01-10 00:34:21 +0000 | [diff] [blame] | 53 | import types |
| 54 | |
Fred Drake | 227b120 | 2000-08-17 05:06:49 +0000 | [diff] [blame] | 55 | class Error(Exception): |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 56 | pass |
| 57 | error = Error # backward compatibility |
Guido van Rossum | 409780f | 1995-01-10 00:34:21 +0000 | [diff] [blame] | 58 | |
Guido van Rossum | f8baad0 | 2000-11-27 21:53:14 +0000 | [diff] [blame] | 59 | try: |
| 60 | from org.python.core import PyStringMap |
| 61 | except ImportError: |
| 62 | PyStringMap = None |
| 63 | |
Guido van Rossum | 6cef6d5 | 2001-09-28 18:13:29 +0000 | [diff] [blame] | 64 | __all__ = ["Error", "error", "copy", "deepcopy"] |
Skip Montanaro | e99d5ea | 2001-01-20 19:54:20 +0000 | [diff] [blame] | 65 | |
Guido van Rossum | 409780f | 1995-01-10 00:34:21 +0000 | [diff] [blame] | 66 | def copy(x): |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 67 | """Shallow copy operation on arbitrary Python objects. |
Guido van Rossum | cc6764c | 1995-02-09 17:18:10 +0000 | [diff] [blame] | 68 | |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 69 | See the module's __doc__ string for more info. |
| 70 | """ |
Guido van Rossum | cc6764c | 1995-02-09 17:18:10 +0000 | [diff] [blame] | 71 | |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 72 | try: |
| 73 | copierfunction = _copy_dispatch[type(x)] |
| 74 | except KeyError: |
| 75 | try: |
| 76 | copier = x.__copy__ |
| 77 | except AttributeError: |
Guido van Rossum | 6cef6d5 | 2001-09-28 18:13:29 +0000 | [diff] [blame] | 78 | try: |
| 79 | reductor = x.__reduce__ |
| 80 | except AttributeError: |
| 81 | raise error, \ |
| 82 | "un(shallow)copyable object of type %s" % type(x) |
| 83 | else: |
| 84 | y = _reconstruct(x, reductor(), 0) |
| 85 | else: |
| 86 | y = copier() |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 87 | else: |
| 88 | y = copierfunction(x) |
| 89 | return y |
Guido van Rossum | 409780f | 1995-01-10 00:34:21 +0000 | [diff] [blame] | 90 | |
| 91 | _copy_dispatch = d = {} |
| 92 | |
| 93 | def _copy_atomic(x): |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 94 | return x |
Guido van Rossum | 409780f | 1995-01-10 00:34:21 +0000 | [diff] [blame] | 95 | d[types.NoneType] = _copy_atomic |
| 96 | d[types.IntType] = _copy_atomic |
| 97 | d[types.LongType] = _copy_atomic |
| 98 | d[types.FloatType] = _copy_atomic |
Guido van Rossum | 8b9def3 | 2001-09-28 18:16:13 +0000 | [diff] [blame] | 99 | try: |
| 100 | d[types.ComplexType] = _copy_atomic |
| 101 | except AttributeError: |
| 102 | pass |
Guido van Rossum | 409780f | 1995-01-10 00:34:21 +0000 | [diff] [blame] | 103 | d[types.StringType] = _copy_atomic |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 104 | try: |
| 105 | d[types.UnicodeType] = _copy_atomic |
| 106 | except AttributeError: |
| 107 | pass |
Guido van Rossum | 2fff84d | 1999-01-25 21:37:02 +0000 | [diff] [blame] | 108 | try: |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 109 | d[types.CodeType] = _copy_atomic |
Guido van Rossum | 2fff84d | 1999-01-25 21:37:02 +0000 | [diff] [blame] | 110 | except AttributeError: |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 111 | pass |
Guido van Rossum | 409780f | 1995-01-10 00:34:21 +0000 | [diff] [blame] | 112 | d[types.TypeType] = _copy_atomic |
| 113 | d[types.XRangeType] = _copy_atomic |
Guido van Rossum | 55d2f39 | 1995-03-14 17:41:36 +0000 | [diff] [blame] | 114 | d[types.ClassType] = _copy_atomic |
Guido van Rossum | 409780f | 1995-01-10 00:34:21 +0000 | [diff] [blame] | 115 | |
| 116 | def _copy_list(x): |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 117 | return x[:] |
Guido van Rossum | 409780f | 1995-01-10 00:34:21 +0000 | [diff] [blame] | 118 | d[types.ListType] = _copy_list |
| 119 | |
| 120 | def _copy_tuple(x): |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 121 | return x[:] |
Guido van Rossum | 409780f | 1995-01-10 00:34:21 +0000 | [diff] [blame] | 122 | d[types.TupleType] = _copy_tuple |
| 123 | |
| 124 | def _copy_dict(x): |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 125 | return x.copy() |
Guido van Rossum | 409780f | 1995-01-10 00:34:21 +0000 | [diff] [blame] | 126 | d[types.DictionaryType] = _copy_dict |
Guido van Rossum | f8baad0 | 2000-11-27 21:53:14 +0000 | [diff] [blame] | 127 | if PyStringMap is not None: |
| 128 | d[PyStringMap] = _copy_dict |
Guido van Rossum | 409780f | 1995-01-10 00:34:21 +0000 | [diff] [blame] | 129 | |
| 130 | def _copy_inst(x): |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 131 | if hasattr(x, '__copy__'): |
| 132 | return x.__copy__() |
| 133 | if hasattr(x, '__getinitargs__'): |
| 134 | args = x.__getinitargs__() |
| 135 | y = apply(x.__class__, args) |
| 136 | else: |
| 137 | y = _EmptyClass() |
| 138 | y.__class__ = x.__class__ |
| 139 | if hasattr(x, '__getstate__'): |
| 140 | state = x.__getstate__() |
| 141 | else: |
| 142 | state = x.__dict__ |
| 143 | if hasattr(y, '__setstate__'): |
| 144 | y.__setstate__(state) |
| 145 | else: |
| 146 | y.__dict__.update(state) |
| 147 | return y |
Guido van Rossum | 409780f | 1995-01-10 00:34:21 +0000 | [diff] [blame] | 148 | d[types.InstanceType] = _copy_inst |
| 149 | |
| 150 | del d |
| 151 | |
| 152 | def deepcopy(x, memo = None): |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 153 | """Deep copy operation on arbitrary Python objects. |
Guido van Rossum | cc6764c | 1995-02-09 17:18:10 +0000 | [diff] [blame] | 154 | |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 155 | See the module's __doc__ string for more info. |
| 156 | """ |
Guido van Rossum | cc6764c | 1995-02-09 17:18:10 +0000 | [diff] [blame] | 157 | |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 158 | if memo is None: |
| 159 | memo = {} |
| 160 | d = id(x) |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 161 | if d in memo: |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 162 | return memo[d] |
| 163 | try: |
| 164 | copierfunction = _deepcopy_dispatch[type(x)] |
| 165 | except KeyError: |
| 166 | try: |
Guido van Rossum | 11ade1d | 2002-06-10 21:10:27 +0000 | [diff] [blame] | 167 | issc = issubclass(type(x), type) |
| 168 | except TypeError: |
| 169 | issc = 0 |
| 170 | if issc: |
| 171 | y = _deepcopy_dispatch[type](x, memo) |
Guido van Rossum | 6cef6d5 | 2001-09-28 18:13:29 +0000 | [diff] [blame] | 172 | else: |
Guido van Rossum | 11ade1d | 2002-06-10 21:10:27 +0000 | [diff] [blame] | 173 | try: |
| 174 | copier = x.__deepcopy__ |
| 175 | except AttributeError: |
| 176 | try: |
| 177 | reductor = x.__reduce__ |
| 178 | except AttributeError: |
| 179 | raise error, \ |
| 180 | "un-deep-copyable object of type %s" % type(x) |
| 181 | else: |
| 182 | y = _reconstruct(x, reductor(), 1, memo) |
| 183 | else: |
| 184 | y = copier(memo) |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 185 | else: |
| 186 | y = copierfunction(x, memo) |
| 187 | memo[d] = y |
Guido van Rossum | 6115460 | 2002-08-12 20:20:08 +0000 | [diff] [blame] | 188 | _keep_alive(x, memo) # Make sure x lives at least as long as d |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 189 | return y |
Guido van Rossum | 409780f | 1995-01-10 00:34:21 +0000 | [diff] [blame] | 190 | |
| 191 | _deepcopy_dispatch = d = {} |
| 192 | |
| 193 | def _deepcopy_atomic(x, memo): |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 194 | return x |
Guido van Rossum | 409780f | 1995-01-10 00:34:21 +0000 | [diff] [blame] | 195 | d[types.NoneType] = _deepcopy_atomic |
| 196 | d[types.IntType] = _deepcopy_atomic |
| 197 | d[types.LongType] = _deepcopy_atomic |
| 198 | d[types.FloatType] = _deepcopy_atomic |
Guido van Rossum | 8b9def3 | 2001-09-28 18:16:13 +0000 | [diff] [blame] | 199 | try: |
| 200 | d[types.ComplexType] = _deepcopy_atomic |
| 201 | except AttributeError: |
| 202 | pass |
Guido van Rossum | 409780f | 1995-01-10 00:34:21 +0000 | [diff] [blame] | 203 | d[types.StringType] = _deepcopy_atomic |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 204 | try: |
| 205 | d[types.UnicodeType] = _deepcopy_atomic |
| 206 | except AttributeError: |
| 207 | pass |
Guido van Rossum | 88b666c | 2002-02-28 23:19:52 +0000 | [diff] [blame] | 208 | try: |
| 209 | d[types.CodeType] = _deepcopy_atomic |
| 210 | except AttributeError: |
| 211 | pass |
Guido van Rossum | 409780f | 1995-01-10 00:34:21 +0000 | [diff] [blame] | 212 | d[types.TypeType] = _deepcopy_atomic |
| 213 | d[types.XRangeType] = _deepcopy_atomic |
| 214 | |
| 215 | def _deepcopy_list(x, memo): |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 216 | y = [] |
| 217 | memo[id(x)] = y |
| 218 | for a in x: |
| 219 | y.append(deepcopy(a, memo)) |
| 220 | return y |
Guido van Rossum | 409780f | 1995-01-10 00:34:21 +0000 | [diff] [blame] | 221 | d[types.ListType] = _deepcopy_list |
| 222 | |
| 223 | def _deepcopy_tuple(x, memo): |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 224 | y = [] |
| 225 | for a in x: |
| 226 | y.append(deepcopy(a, memo)) |
| 227 | d = id(x) |
| 228 | try: |
| 229 | return memo[d] |
| 230 | except KeyError: |
| 231 | pass |
| 232 | for i in range(len(x)): |
| 233 | if x[i] is not y[i]: |
| 234 | y = tuple(y) |
| 235 | break |
| 236 | else: |
| 237 | y = x |
| 238 | memo[d] = y |
| 239 | return y |
Guido van Rossum | 409780f | 1995-01-10 00:34:21 +0000 | [diff] [blame] | 240 | d[types.TupleType] = _deepcopy_tuple |
| 241 | |
| 242 | def _deepcopy_dict(x, memo): |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 243 | y = {} |
| 244 | memo[id(x)] = y |
Raymond Hettinger | e0d4972 | 2002-06-02 18:55:56 +0000 | [diff] [blame] | 245 | for key, value in x.iteritems(): |
| 246 | y[deepcopy(key, memo)] = deepcopy(value, memo) |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 247 | return y |
Guido van Rossum | 409780f | 1995-01-10 00:34:21 +0000 | [diff] [blame] | 248 | d[types.DictionaryType] = _deepcopy_dict |
Guido van Rossum | f8baad0 | 2000-11-27 21:53:14 +0000 | [diff] [blame] | 249 | if PyStringMap is not None: |
| 250 | d[PyStringMap] = _deepcopy_dict |
Guido van Rossum | 409780f | 1995-01-10 00:34:21 +0000 | [diff] [blame] | 251 | |
Guido van Rossum | 558be28 | 1997-08-20 22:26:19 +0000 | [diff] [blame] | 252 | def _keep_alive(x, memo): |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 253 | """Keeps a reference to the object x in the memo. |
Guido van Rossum | 558be28 | 1997-08-20 22:26:19 +0000 | [diff] [blame] | 254 | |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 255 | Because we remember objects by their id, we have |
| 256 | to assure that possibly temporary objects are kept |
| 257 | alive by referencing them. |
| 258 | We store a reference at the id of the memo, which should |
| 259 | normally not be used unless someone tries to deepcopy |
| 260 | the memo itself... |
| 261 | """ |
| 262 | try: |
| 263 | memo[id(memo)].append(x) |
| 264 | except KeyError: |
| 265 | # aha, this is the first one :-) |
| 266 | memo[id(memo)]=[x] |
Guido van Rossum | 558be28 | 1997-08-20 22:26:19 +0000 | [diff] [blame] | 267 | |
Guido van Rossum | 409780f | 1995-01-10 00:34:21 +0000 | [diff] [blame] | 268 | def _deepcopy_inst(x, memo): |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 269 | if hasattr(x, '__deepcopy__'): |
| 270 | return x.__deepcopy__(memo) |
| 271 | if hasattr(x, '__getinitargs__'): |
| 272 | args = x.__getinitargs__() |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 273 | args = deepcopy(args, memo) |
| 274 | y = apply(x.__class__, args) |
| 275 | else: |
| 276 | y = _EmptyClass() |
| 277 | y.__class__ = x.__class__ |
| 278 | memo[id(x)] = y |
| 279 | if hasattr(x, '__getstate__'): |
| 280 | state = x.__getstate__() |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 281 | else: |
| 282 | state = x.__dict__ |
| 283 | state = deepcopy(state, memo) |
| 284 | if hasattr(y, '__setstate__'): |
| 285 | y.__setstate__(state) |
| 286 | else: |
| 287 | y.__dict__.update(state) |
| 288 | return y |
Guido van Rossum | 409780f | 1995-01-10 00:34:21 +0000 | [diff] [blame] | 289 | d[types.InstanceType] = _deepcopy_inst |
| 290 | |
Guido van Rossum | 1e91c14 | 2001-12-28 21:33:22 +0000 | [diff] [blame] | 291 | def _reconstruct(x, info, deep, memo=None): |
Guido van Rossum | 6cef6d5 | 2001-09-28 18:13:29 +0000 | [diff] [blame] | 292 | if isinstance(info, str): |
| 293 | return x |
| 294 | assert isinstance(info, tuple) |
Guido van Rossum | 1e91c14 | 2001-12-28 21:33:22 +0000 | [diff] [blame] | 295 | if memo is None: |
| 296 | memo = {} |
Guido van Rossum | 6cef6d5 | 2001-09-28 18:13:29 +0000 | [diff] [blame] | 297 | n = len(info) |
| 298 | assert n in (2, 3) |
| 299 | callable, args = info[:2] |
| 300 | if n > 2: |
| 301 | state = info[2] |
| 302 | else: |
| 303 | state = {} |
| 304 | if deep: |
Guido van Rossum | 1e91c14 | 2001-12-28 21:33:22 +0000 | [diff] [blame] | 305 | args = deepcopy(args, memo) |
Guido van Rossum | 6cef6d5 | 2001-09-28 18:13:29 +0000 | [diff] [blame] | 306 | y = callable(*args) |
| 307 | if state: |
| 308 | if deep: |
Guido van Rossum | 1e91c14 | 2001-12-28 21:33:22 +0000 | [diff] [blame] | 309 | state = deepcopy(state, memo) |
Guido van Rossum | 3e3583c | 2002-06-06 17:41:20 +0000 | [diff] [blame] | 310 | if hasattr(y, '__setstate__'): |
| 311 | y.__setstate__(state) |
| 312 | else: |
| 313 | y.__dict__.update(state) |
Guido van Rossum | 6cef6d5 | 2001-09-28 18:13:29 +0000 | [diff] [blame] | 314 | return y |
| 315 | |
Guido van Rossum | 409780f | 1995-01-10 00:34:21 +0000 | [diff] [blame] | 316 | del d |
| 317 | |
| 318 | del types |
| 319 | |
Guido van Rossum | c5d2d51 | 1997-12-07 16:18:22 +0000 | [diff] [blame] | 320 | # Helper for instance creation without calling __init__ |
| 321 | class _EmptyClass: |
| 322 | pass |
| 323 | |
Guido van Rossum | 409780f | 1995-01-10 00:34:21 +0000 | [diff] [blame] | 324 | def _test(): |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 325 | l = [None, 1, 2L, 3.14, 'xyzzy', (1, 2L), [3.14, 'abc'], |
| 326 | {'abc': 'ABC'}, (), [], {}] |
| 327 | l1 = copy(l) |
| 328 | print l1==l |
| 329 | l1 = map(copy, l) |
| 330 | print l1==l |
| 331 | l1 = deepcopy(l) |
| 332 | print l1==l |
| 333 | class C: |
| 334 | def __init__(self, arg=None): |
| 335 | self.a = 1 |
| 336 | self.arg = arg |
| 337 | if __name__ == '__main__': |
| 338 | import sys |
| 339 | file = sys.argv[0] |
| 340 | else: |
| 341 | file = __file__ |
| 342 | self.fp = open(file) |
| 343 | self.fp.close() |
| 344 | def __getstate__(self): |
| 345 | return {'a': self.a, 'arg': self.arg} |
| 346 | def __setstate__(self, state): |
Raymond Hettinger | e0d4972 | 2002-06-02 18:55:56 +0000 | [diff] [blame] | 347 | for key, value in state.iteritems(): |
| 348 | setattr(self, key, value) |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 349 | def __deepcopy__(self, memo = None): |
| 350 | new = self.__class__(deepcopy(self.arg, memo)) |
| 351 | new.a = self.a |
| 352 | return new |
| 353 | c = C('argument sketch') |
| 354 | l.append(c) |
| 355 | l2 = copy(l) |
| 356 | print l == l2 |
| 357 | print l |
| 358 | print l2 |
| 359 | l2 = deepcopy(l) |
| 360 | print l == l2 |
| 361 | print l |
| 362 | print l2 |
| 363 | l.append({l[1]: l, 'xyz': l[2]}) |
| 364 | l3 = copy(l) |
| 365 | import repr |
| 366 | print map(repr.repr, l) |
| 367 | print map(repr.repr, l1) |
| 368 | print map(repr.repr, l2) |
| 369 | print map(repr.repr, l3) |
| 370 | l3 = deepcopy(l) |
| 371 | import repr |
| 372 | print map(repr.repr, l) |
| 373 | print map(repr.repr, l1) |
| 374 | print map(repr.repr, l2) |
| 375 | print map(repr.repr, l3) |
Guido van Rossum | 409780f | 1995-01-10 00:34:21 +0000 | [diff] [blame] | 376 | |
| 377 | if __name__ == '__main__': |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 378 | _test() |