copy operations in python

The copy module in python provides copy() and deepcopy() functions which support shallow copy and deep copy operations of objects.

Copy Updated 21 Jan 2017 1 min read

Overview

  • With the following assignment statement

b = a

Python will not make a copy of the object as referenced to by identifier "a"

and make the identifier "b" to to reference the newly created copy.

  • Using the assignment operator Python will only copy the reference contained in identifier "a" to identifier "b". This makes the identifier "b" as well to reference to the original object referenced to by "a".

  • To facilitate copying of objects python standard library has defined the copy functions in the module "copy".

Assignment operation on Python

The python module copy has the following functions:

copy(object)Returns a shallow copy of the object that is passed as the parameter
deepcopy(object)Returns a deep copy of the object that is passed as the parameter