Movatterモバイル変換


[0]ホーム

URL:


Previous PageUp One LevelNext PagePython Library ReferenceContentsModule IndexIndex
Previous:3.17 shelveUp:3. Python Runtime ServicesNext:3.19 marshal

3.18copy -- Shallow and deep copy operations

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
For module specific errors,copy.error is raised.

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:

Modulepickle:
Discussion of the special methods used tosupport object state retrieval and restoration.


Previous PageUp One LevelNext PagePython Library ReferenceContentsModule IndexIndex
Previous:3.17 shelveUp:3. Python Runtime ServicesNext:3.19 marshal
Release 2.2.3, documentation updated on 30 May 2003.
SeeAbout this document... for information on suggesting changes.
[8]ページ先頭

©2009-2026 Movatter.jp