[Edu-sig] Pass by Reference

David MacQuigg macquigg at ece.arizona.edu
Thu May 15 21:41:51 CEST 2008


At 04:30 PM 5/14/2008 -0500, John Zelle wrote:

>At some point, I have to just let this go, as I think we all on this
>list have a pretty good understanding of the differences between C and
>Python in terms of assignment and parameter passing. But let's _not_ use
>the term "pass by reference" when talking about Python. You CANNOT
>CHANGE THE CONTENTS OF THE VARIABLE THAT IS SUPPLIED AS AN ACTUAL
>PARAMETER. It will still refer to the same object (contain the same
>reference) regardless of what is done to the formal parameter. Hence,
>the variable IS NOT passed by reference. The value of the variable
>(which happens to be a reference) is copied. Pass by value is the
>accepted label for this mechanism. Pass by reference means something
>else (as I've pointed out in previous posts).
>
>Christopher's point on teaching the differences directly is, I think, a
>good one. But we don't need to worry about that for beginners.

I agree, the topic should not come up in a class for beginners, but the question I got was from a student in a class on C.  In that situation, it does help to explain Python's calling mechanism in terms that these students understand.  I've posted my answer at http://ece.arizona.edu/~edatools/ece175/Lecture/QnA.txt

I used the phrase "like pass-by-reference" to avoid the controversy as much as I can.  As you can see from the references cited in my answer, the terms call-by-value and call-by-reference do not have universally accepted definitions, even among experts.

The differences seem to come down to what we mean by the words "value" and "reference".  Everyone seems to have unstated assumptions about their meaning, probably depending on what language they are thinking about.  Perhaps it will help if we can agree on models for variables in C and Python.  Here are my models:

In C, the type of an "object" (char, int, float, etc.) is associated with
the variable name, not the object.

   variable:(name, type, pointer) --> object:(value, address)

In Python, type is associated with the object, not the variable name.

   variable:(name, pointer) --> object:(type, value, address)

As you can see, I've used the word "value" to mean the actual data associated with the object, and the word "pointer" to mean whatever it is that references the object (a real pointer in C, I assume a C-pointer in Python).

I realize my diagrams may bias the discussion, so perhaps I should stop at this point and ask for alternative models or terminology.

-- Dave




More information about the Edu-sig mailing list