James's Ramblings

Python: Copying Objects

Created: June 03, 2020

=

When a new reference variable is created from another using the = operator, a new object is not created. Rather, the new reference variable points to exactly the same object. To create a copy of an object, the copy module needs to be used.

import copy

import copy

Shallow copies

Shallow copies (copy()) are not recursive and do not copy objects nested within the initial object.

Deep copies

Deep copies (deepcopy()) are recursive.