Skip to content

Latest commit

 

History

History
32 lines (25 loc) · 915 Bytes

File metadata and controls

32 lines (25 loc) · 915 Bytes

Deep Copy a Nested Object

To create an independent copy of a nested object, use the deepcopy method from the copy module.

>>> obj = [['a','b','c'],['d','e','f'],['g','h']]
>>> import copy
>>> deep_copy = copy.deepcopy(obj)
>>> deep_copy
[['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h']]
>>> deep_copy is obj
False
>>>

Setting objects equal will normally create a shallow copy.

>>> obj = [['a','b','c'],['d','e','f'],['g','h']]
>>> shallow_copy = obj
>>> shallow_copy
[['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h']]
>>> shallow_copy is obj
True
>>>

Any changes to the object that were a part of the operation that created shallow_copy will be shared between both variables. Both obj and shallow_copy point to the same objects in Python.

References

Found in Section 4.4 pf the book Python Tricks The Book by Dan Bader.