two quick questions

Erik Max Francis max at alcyone.com
Wed Aug 13 02:20:11 EDT 2003


Elaine Jackson wrote:

> 1) Does Python have passing-by-reference?

It depends on exactly what you mean by that.  In a sense all Python
objects are passed by reference, but only in the sense that the
reference is passed by value.  (Say that three times fast.)

If you want to get the equivalent of a C++ reference on an immutable
object, you can do it with containment.  Pass the function a mutable
container containing your object, and then manipulate/change the
contained object.  In the caller's scope, the container will have
mutated.

> 2) In ordinary parlance, "deep" implies "shallow" but not conversely.
> In the
> Python "copy" module (if I understand correctly), the implication goes
> the other
> way. Do you find this a nuisance?

I'm not sure what about the copy's modules semantics you're thinking are
reversed, but the terminology used in the copy module is common in
computer science.  A shallow copy means that the object is copied, but
it will retain the same references to contained objects; a deep copy
means that the object is copied, as well as the objects it contains (and
so on, recursively).  A deep copy always does the same thing as a
shallow copy, and more.

-- 
   Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/
 __ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/  \ I always entertain great hopes.
\__/  Robert Frost




More information about the Python-list mailing list