Does deepcopy use copy to write?

I wonder if the python interpreter applies copying to the write strategy when doing a deep copy on mutable objects.

Also, I would like to know if a deep copy is also being executed on a nonmutable object (which may seem strange to me)

+5
source share
3 answers

It does not do copy-on-write.

It does not make a deep copy of some built-in immutable types, but any user-defined "immutable" types will be deeply copied.

copy.py in the Python 2.7 standard library contains this post in its documentation:

, , , , , , , , , , - .

copy :

def _copy_immutable(x):
    return x
for t in (type(None), int, long, float, bool, str, tuple,
          frozenset, type, xrange, types.ClassType,
          types.BuiltinFunctionType, type(Ellipsis),
          types.FunctionType, weakref.ref):
    d[t] = _copy_immutable
for name in ("ComplexType", "UnicodeType", "CodeType"):
    t = getattr(types, name, None)
    if t is not None:
        d[t] = _copy_immutable

deepcopy , , . , _deepcopy_tuple , , .

for i in range(len(x)):
    if x[i] is not y[i]:
        y = tuple(y)
        break
else:
    y = x
+5

, , . , .

+4

:

>>> import copy
>>> x = [[1],[2],"abc"]
>>> y = copy.deepcopy(x)
>>> id(x[0])
4299612960
>>> id(y[0])
4299541608
>>> id(x[2])
4297774504
>>> id(y[2])
4297774504

x y , . , , .

+3

All Articles