[Edu-sig] Pass by Reference

John Zelle john.zelle at wartburg.edu
Tue May 20 18:10:25 CEST 2008


Hi John,

I can't agree with this.

On Tue, 2008-05-20 at 10:50 -0400, John Posner wrote:
> > ... and stop trying to invent new names for a parameter passing mechanism
> > that is identical in function to traditional call by value.
> > 
> 
> Yeah, but ... it will be difficult to stick to a call-by-value
> characterization when confronted with this example, which is straight from
> "Call by Reference 101":
> 
> def AddArtist(mylist):
>     mylist.append('TheOtherTerry')
>  
> >>> troupe
> ['Graham', 'John', 'Eric', 'Michael', 'Terry']
> 
> >>> AddArtist(troupe)
> 
> >>> troupe
> ['Graham', 'John', 'Eric', 'Michael', 'Terry', 'TheOtherTerry']

Great example, but the result here has NOTHING WHATSOEVER to do with
parameter passing by reference. If I do this:

mylist = troupe
myList.append("TheOtherTerry")

I get exactly the same result. That's because the value of a variable in
Python IS A REFERENCE. Aliasing is aliasing, and it has nothing to do in
this case with how parameters are passed.

As I keep explaining, when the term pass-by-reference is applied to
parameter passing, it means that the formal parameter becomes a
reference to the actual variable used in the argument (more technically,
the formal parameter name is bound to the storage location of the
argument variable). That means changes to the value of the formal
parameter actually change the contents of the calling variable. Python
does not do this! You can change the state of the object that's referred
to, but you _cannot change the contents of the variable_ (what it refers
to).

The example you give illustrates what happens with call-by-value in _any
language_ that allows the value of a variable to be a reference
(pointer). Java has no "reference parameters" or "call by reference"
mechanism, but it exhibits exactly the same behavior (when the argument
is a reference). Similarly in C, if I pass a pointer and change the
state of the object that it points to, then the change will be visible
to the caller even though the pointer itself is passed-by-value. The C
trick of sending an address of the variable (&foo) is a way of
simulating or "effecting" call by reference. Technically, the parameter
is still passed by value. The programmer must send the reference and
also do the appropriate dereferencing in the function (using
*formal_param).

Perhaps the confusion comes because these other languages allow some
variables to be references and others not. In Python all variable values
are references. Furthermore Python has no C-like trick to effect
pass-by-reference, because even if you can get the value of a variable's
contents (say by doing id(a)) you cannot set up a way for a different
variable to change that value (even if b = a, setting b = c cannot
change the contents of a). 

This mechanism (pass-by-value) is distinct from pass-by-reference as
found in languages like FORTRAN, Pascal, or (arguably) C++.

> Most students (especially newbies) won't care about what happens under the
> hood -- not at first. If it looks like a duck, walks like a duck, and quacks
> like a duck ...

First up, there is no reason to discuss these distinctions in detail
with newbies. Tell them how it works. That's all you need to do. Newbies
won't give a hoot about call-by-reference or call-by-value, as they have
not yet learned those terms. But don't teach them the wrong term!
Second, it doesn't quack like a reference parameter because Python
doesn't allow changing the value of the callers variable!!! It walks and
quacks exactly like a variable whose value is a reference being
passed-by-value. So if you insist on giving the newbie a label, give
them the right label. The _parameter_ passing_ mechanism is plain old
pass-by-value.

When dealing with more experienced folks, explain that all Python
variables are references. That's what's different from (most)
traditional languages. That explains many things about the Python. Then
when you tell them parameters are passed by value, they will understand
the implications of that. Another reference to the same object allows
changes in the object state through one to be visible to the other. But
please don't pretend that the parameter passing mechanism is some
strange and bizarre hybrid and not like other languages, because that's
just not true.

--John

ps. OK, now I'm letting go of the issue.

> 
> -John
> 
> _______________________________________________
> Edu-sig mailing list
> Edu-sig at python.org
> http://mail.python.org/mailman/listinfo/edu-sig
> 
-- 
John M. Zelle, Ph.D.             Wartburg College
Professor of Computer Science    Waverly, IA     
john.zelle at wartburg.edu          (319) 352-8360  



More information about the Edu-sig mailing list