|
Size: 1660
Comment:
|
← Revision 5 as of 2023-10-10 20:43:25 ⇥
Size: 1620
Comment:
|
| Deletions are marked like this. | Additions are marked like this. |
| Line 11: | Line 11: |
| == Example == | == Concept == A ''deep copy'' differs from a ''shallow copy'' only for containers. A shallow copy contains references to the same objects that the original contains. A deep copy contains copies of the original's contents. This poses two challenges: 1. Recursive objects may contain a reference to themselves, and causing a recursion loop 2. Some data should be copied once and shared among the contents Creating a deep copy then involves: 1. Allowing objects to override the operation to avoid loops; see [[Python/DunderMethod#__DeepCopy__|__deepcopy__]] for details 2. Keeping a `memo` dictionary for data already copied, so that it can be shared among the contents ---- == Usage == Two functions are exposed: '''`copy()`''' and '''`deepcopy()`'''. To create a deep copy, try: |
| Line 40: | Line 62: |
| == Concept == A ''deep copy'' differs from a ''shallow copy'' only for containers. A shallow copy contains references to the same objects that the original contains. A deep copy contains copies of the original's contents. This poses two challenges: 1. Recursive objects may contain a reference to themselves, and causing a recursion loop 2. Some data should be copied once and shared among the contents Creating a deep copy then involves: 1. Allowing objects to override the operation to avoid loops; see [[Python/DunderMethod#__DeepCopy__|__deepcopy__]] for details 2. Keeping a `memo` dictionary for data already copied, so that it can be shared among the contents ---- == Usage == === Copy === Make a shallow copy of an object. === DeepCopy === Make a deep copy of an object. ---- |
Python Copy
copy is a module for deep- and shallow-copying operations. This is useful for any objects that are passed by reference, especially lists.
Contents
Concept
A deep copy differs from a shallow copy only for containers. A shallow copy contains references to the same objects that the original contains. A deep copy contains copies of the original's contents.
This poses two challenges:
- Recursive objects may contain a reference to themselves, and causing a recursion loop
- Some data should be copied once and shared among the contents
Creating a deep copy then involves:
Allowing objects to override the operation to avoid loops; see __deepcopy__ for details
Keeping a memo dictionary for data already copied, so that it can be shared among the contents
Usage
Two functions are exposed: copy() and deepcopy().
To create a deep copy, try:
import copy
class foo(object):
def __init__(self, name):
self.name = name
def __eq__(self, other):
return self.name == other.name
a = foo("foo")
lista = [a]
listb = lista
lista[0] == listb[0] # True
lista[0] is listb[0] # True
listc = copy.deepcopy(lista)
lista[0] == listc[0] # True
lista[0] is listc[0] # False
See also
Python copy module documentation
Python Module of the Week article for copy
