Tech notes

Daily notes about my IT activities.

2012-03-27

python: shallow and deep copy operations

by hackprime

Shallow copy - copies a primitive types values and links on complex objects.

>>> a = [1, 2]
>>> from copy import copy
>>> b = [1, 'a', True, a]
>>> c = copy(b)
>>> a.append(33)
>>> c
[1, 'a', True, [1, 2, 33]]

Deep copy - full copy of all nested objects

>>> a = [1, 2]
>>> from copy import deepcopy
>>> b = [1, 'a', True, a]
>>> c = deepcopy(b)
>>> a.append(33)
>>> c
[1, 'a', True, [1, 2]]

```

Source: Python Docs - Shallow and deep copy operations