深度复制是否使用写时复制?

发布于 2024-11-26 15:14:30 字数 91 浏览 4 评论 0原文

我想知道 python 解释器在对可变对象进行深度复制时是否应用写时复制策略。

另外,我想知道深度复制是否也在不可变对象上执行(但这对我来说似乎很奇怪)

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

Also, I'd like to know if the deepcopy is performed also on nonmutable object (that would seem strange to me however)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

久隐师 2024-12-03 15:14:30

它不进行写时复制。

它不会对某些内置不可变类型进行深层复制,但任何用户定义的“不可变”类型都将被深层复制。

Python 2.7 标准库中的 copy.py 包含此消息它的文档:

此版本不复制模块、类、函数、方法等类型,也不复制堆栈跟踪、堆栈帧、文件、套接字、窗口、数组或任何类似类型。

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

It does not do copy-on-write.

It doesn't do a deep copy on some built-in immutable types, but any user-defined "immutable" types will be deep-copied.

copy.py in the Python 2.7 standard library includes this message in its documentation:

This version does not copy types like module, class, function, method, nor stack trace, stack frame, nor file, socket, window, nor array, nor any similar types.

copy handles immutable objects like this:

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 uses a more complicated scheme that's too long to copy into this most, but the gist is the same. One interesting point is that _deepcopy_tuple iterates through its elements and don't create a new object until it finds an element that was copied.

for i in range(len(x)):
    if x[i] is not y[i]:
        y = tuple(y)
        break
else:
    y = x
猛虎独行 2024-12-03 15:14:30

不,不是的,只是复制对象。如果不可变对象引用可变对象,它也必须复制它们。

No, it doesn't it, just copies the objects. And it also must copy immutable objects if they reference mutables.

ㄖ落Θ余辉 2024-12-03 15:14:30

让我们看看:

>>> 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

对于xy的第一个元素,执行了复制,对象有了新的id。第三个元素是一个不可变的字符串,不会被复制。

Let's see:

>>> 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

For the first element of x and y, the copy is performed and the object has a new id. The third element, an immutable string, is not copied.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文