This module provides generic (shallow and deep) copying operations.
Interface summary:
import copy x = copy.copy(y) # make a shallow copy of y x = copy.deepcopy(y) # make a deep copy of y
The difference between shallow and deep copying is only relevant for compound objects (objects that contain other objects, like lists or class instances):
Two problems often exist with deep copy operations that don't exist with shallow copy operations:
Python's deepcopy() operation avoids these problems by:
This version does not copy types like module, class, function, method, nor stack trace, stack frame, nor file, socket, window, nor array, nor any similar types.
Classes can use the same interfaces to control copying that they use to control pickling: they can define methods called __getinitargs__(), __getstate__() and __setstate__(). See the description of module pickle for information on these methods.