Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

References and copies

Assignment binds another name to an existing object without copying it.

Version: Python 3.14; repository image, production runtime not attested

Syntax

copy.deepcopy(value)

Assignment binds another name to an existing object without copying it.

A shallow copy creates a new outer container while retaining references to its children. A deep copy recursively copies supported nested data and uses a memo to preserve repeated references within the new graph.

The example contrasts editing a shared child with rebinding an outer list slot.

Examples

from copy import deepcopy
child = [1]
base = [child, child]
alias = base
shallow = base.copy()
deep = deepcopy(base)
shallow[0].append(2)
shallow[1] = ["new"]
print(alias is base, base, shallow)
print(deep, deep[0] is deep[1], deep[0] is child)

Result

True [[1, 2], [1, 2]] [[1, 2], ['new']]
[[1], [1]] True False

The example contrasts editing a shared child with rebinding an outer list slot.

Sources