Copy operator (was list.remove for Noivces)

Chris Barker chrishbarker at home.net
Wed Nov 28 15:43:04 EST 2001


arthur.siegel at rsmi.com wrote:
> One imports copy, like one imports cgi or tokenize. To a beginner, imports
> from outside  modules  is strictly special purpose stuff. In my case it was a
> long time before much beyond math ,os and sys perhaps, were anything
> more than absolute exotica.

If anything, this is a good argument for making copy() and deepcopy() a
built-in, rather than part of a mmodule. The funny thing is: I have
hardly ever used the copy module. In fact, since I discovered that NumPy
arrays have a copy() method, I don't hink I have used it at all. I have
no idea if I am typical, but Python's "everything is a reference"
approach is simple and powerful to me, and I find I have littel use for
copies.

> a) I could be missing something  (but I would need convincing) 

You make it clear that you are with this statement:

> The shallow/deep copy issue I understand is there and real - but I don't
> really understand the issue itself.


>I will do some homework.

Here is a little example:

>>> import copy
>>> a = [1,2,3]
>>> b = [4,5,6]
# a and b are bound to lists of integers, a concept that is pretty easy
for newbies to grasp
>>> c = [a,b]
>>> c
[[1, 2, 3], [4, 5, 6]]
# c is now bound to a list that contains two other lists (the ones bound
to a and b)
>>> d = copy.copy(c)
# d is bound to a copy of the list c is bound to.
>>> e = copy.deepcopy(c)
# e is bound to a deep copy of the list that c is bound to
>>> d
[[1, 2, 3], [4, 5, 6]]
>>> e
[[1, 2, 3], [4, 5, 6]]
# they look the same, so what is the difference?
# let's say we change the list that a is bound to.
>>> a.reverse()
>>> d
[[3, 2, 1], [4, 5, 6]]
# d contains that same list, so it has changed
>>> e
[[1, 2, 3], [4, 5, 6]]
# e contains a copy of that list, so it is not changed.
# so, in what sense is d a copy at all?
>>> c[0] = 6
>>> c
[6, [4, 5, 6]]
# c has now changed
>>> d
[[3, 2, 1], [4, 5, 6]]
# but d hasn't


What I get from this is that someone using Python had better understand
how Python name binding, mutable types, copying, and deep copying work
in order to not get bit eventually. I don't think making it easier tao
make a trivial copy will help that, rather it will hinder it. If there
is something you need to learn, the sooner the better!

-Chris


-- 
Christopher Barker,
Ph.D.                                                           
ChrisHBarker at home.net                 ---           ---           ---
http://members.home.net/barkerlohmann ---@@       -----@@       -----@@
                                   ------@@@     ------@@@     ------@@@
Oil Spill Modeling                ------   @    ------   @   ------   @
Water Resources Engineering       -------      ---------     --------    
Coastal and Fluvial Hydrodynamics --------------------------------------
------------------------------------------------------------------------



More information about the Python-list mailing list