This module provides generic (shallow and deep) copying operations.
Interface summary:
import copyx = copy.copy(y) # make a shallow copy of yx = copy.deepcopy(y) # make a deep copy of y
The difference between shallow and deep copying is only relevant forcompound objects (objects that contain other objects, like lists orclass instances):
Two problems often exist with deep copy operations that don't existwith shallow copy operations:
Thedeepcopy() function avoids these problems by:
This version does not copy types like module, class, function, method,stack trace, stack frame, file, socket, window, array, or any similartypes.
Classes can use the same interfaces to control copying that they useto control pickling: they can define methods called__getinitargs__(),__getstate__() and__setstate__(). See the description of modulepickle for information on thesemethods. Thecopy module does not use thecopy_reg registration module.
In order for a class to define its own copy implementation, it candefine special methods__copy__() and__deepcopy__(). The former is called to implement theshallow copy operation; no additional arguments are passed. Thelatter is called to implement the deep copy operation; it is passedone argument, the memo dictionary. If the__deepcopy__()implementation needs to make a deep copy of a component, it shouldcall thedeepcopy() function with the component as firstargument and the memo dictionary as second argument.
See Also:
| Python Library Reference |