Copy Operations In Python

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


Copyright 2022 © pythontic.com