Assignment, Shallow Copy (Copy) and Deep Copy
- Assignment: Just add one more reference to the origin object
- Shallow Copy: Or just Copy, which will just copy top level object, but will not copy sub-level object (add one more reference for sub-level object)
- Deep Copy: Completely copy the whole object
Example using dictionary
1 | >>>import copy |
Explanation
b=a
: assignment,a
andb
will point to the same objectb=a.copy()
: shallow copy,a
andb
are independent object, however, their sub-objects still point to the same object.b=a.deepcopy()
: deep copy,a
andb
are totally independent object, at any level.