"always passes by reference"

Greg Weeks weeks at golden.dtc.hp.com
Fri Jul 28 11:20:22 EDT 2000


Donn Cave (donn at u.washington.edu) wrote:
: Your account of the system doesn't work for me.

:     def f(x, t):
:         x = t

: Where is the "the variable x"?  Did f() just write a different object
: into it?  Well, in any practical sense, x does have a new value.

The variable x is a block of memory on the execution stack (although that
is more detail than a programmer needs to know), and the assignment "x = t"
does indeed write a different object into it.  However, call-by-value
refers to what happens to a different variable:

	z = 0
	f(z, 1)

In the above code does a new object -- and remember, when I say "object" I
mean an address -- get written into z on the second line?  The answer is
no.


Regards,
Greg


PS to all: Once you distinguish between a word and a writing of a word, the
remaining question is whether you prefer to define an "object" as 1) a
region of memory or 2) an address.  #2 may grate on your intuition, but
otherwise it simplifies discussions of what is happening.

You might say that I (and others!) simply use the term "object" when other
people say "reference".  That is almost true.  But the people who like the
term "reference" also say that there can be several references to a single
object.  This is confused.  If an object is a region of memory, it has only
one address.  So if there are multiple references, then a "reference" is a
*writing* of an address.  I find it confusing to have an expression for a
writing of a word but not for the word itself.



More information about the Python-list mailing list