Deepcopy vs Shallow copy
Shallow copy
Shallow copy, means not deep enough, means internal object isn’t copied. Object in both old container and new container will change together.
obj will change together in obj, var and var_cp in below example.
Tip
Internal objects (objects that contain other objects, like lists or class instances)
interanl = {'a': 3}
ls = [internal, 1, 2, 3]
Here internal is an internal object.
Example
obj = {'a': 3}
var = [obj, 1, 2, 3]
var_cp = var[:]
obj['a'] = 4
>>> var
>>> var_cp
[{'a': 4}, 1, 2, 3]
[{'a': 4}, 1, 2, 3]
>>> var is var_cp
False
>>> var == var_cp
True
Deep copy
Deepcopy create totally independent object from top to bottom. You could do deepcopy by builtin module copy.
from copy import deepcopy
obj = {'a':1}
var = [obj, 1, 2, 3]
var_dpcp = deepcopy(var)
var[0]['a'] = 2
>>> var_dpcp
[{'a': 1}, 1, 2, 3]
>>> var is var_dpcp
False
>>> var == var_dpcp
True
Reference
Reference is just a alternative name of orignal object. Assignment create a reference.
obj = {'a':1}
var = [obj, 1, 2, 3]
var_ref = var
var[1] = -1
var[0]['a'] = 2
>>> var_ref
[{'a': 2}, -1, 2, 3]
>>> var is var_ref
True
Tip
operator |
meaning |
|---|---|
|
equal |
|
not equal |
|
object identity |
|
negative object identity |