thinkings on shallow copy

Kendear kendear at nospam.com
Thu Jun 19 12:38:22 CEST 2003


Kendear wrote:
> i was just looking at docstrings and saw the one
> for copy.__doc__ regarding copy.copy():
> 
>  > - A shallow copy constructs a new compound object and then (to the
>  >   extent possible) inserts *the same objects* into in that the
>  >   original contains.
> 
> I wonder if it is actually to insert *the same object references*
> into the new compound object...


so for the same reason, for deepcopy in the docstring:

 > - A deep copy constructs a new compound object and then, recursively,
 >   inserts *copies* into it of the objects found in the original.

it constructs a new compound object, and set its
elements to point to newly created objects (mirroring the source)
These newly created objects are created because they are mutable
(list, dictionary) or they may contain mutable objects some level
down (tuple).  It does so recursively.  For the absolutely immutable
object (immutable any level down), it doesn't have to create
new objects for them. (such as numbers, strings, and functions)
and can directly point to them.





More information about the Python-list mailing list