"always passes by reference"

Greg Weeks weeks at golden.dtc.hp.com
Thu Jul 27 18:27:37 EDT 2000


Tim Peters (tim_one at email.msn.com) wrote:
: [Greg Ewing] 
: > Whether you call it that or not, there has to be something
: > in the model that is not-the-object but somehow identifies-
: > the-object, so that the object can appear in more than one
: > place at a time without having multiple copies of it.

Yes, but this something exists in spoken languages as well as computer
languages, and it is confusing to create new nomenclature for it.


1.  Please consider words in the English language.  Suppose (for the sake
of argument) that there are 500,000 English words.  If I now write the word
"dog" 10 times

    dog dog dog dog dog dog dog dog dog dog

have I added 10 words to the English language?  No.  The writing of a word
does not create another word.  It creates another writing.  Each writing
clearly identifies the word, but people don't say that the writing is a
"reference" to the word.  They *could* say this -- but since they don't, I
don't want to either.


2.  Next consider the integer type as implemented in C.  A C integer is a
binary word consisting (typically) of 32 bits, eg:

	000000000000000000000000000101010

As far as C is concerned, this word is the integer 42.  This word can be
written into multiple memory locations.  Each writing clearly identifies
42, but it sounds odd to say that each writing is a "reference" to 42.


3.  Given the above discussion: As long as data objects in the computer are
implemented as binary words, it is clear that an object can be written to
multiple memory locations without replicating the object itself.  So how
*are* objects implemented?  For definiteness, considered structured objects
like lists and dictionaries.

In the C world, it is declared that an object is a region of storage.
However, a region of storage has a unique address.  So we can just as well
declare that an object is the *address* of a region of storage.

And that is what works for me!  (And for the old Lisp community as well.)
The way I see it, in Python every object is an address.  Objects are thus
binary words that can be written to multiple memory locations.  Objects can
be assigned to variables, passed to functions, and returned from functions.

Also, the function call f(x) is by-value.  The function receives the object
that has been written into the variable x.  If the object is mutable -- ie,
if there is some way to modify the region of storage addressed by the
object -- then the function may change the *state* of the object.  But the
function can not write a different object into the variable x.


So, the distinction between "objects" and "references to objects" is not
*necessary*.  Whether or not it is *desirable* is to some degree a matter
of taste.

Regards,
Greg



More information about the Python-list mailing list