Pass by reference ?
Gordon McMillan
gmcm at hypernet.com
Wed Apr 5 21:45:42 EDT 2000
Robert W. Cunningham wrote:
> Gordon McMillan wrote:
>
> > Robert W. Cunningham wrote:
> >
> > > The CS models of "Pass By Reference" and "Pass By Value" are fairly well understood,
> > > but they do not seem to map simply and directly to Python.
> >
> > Grrr. Pascal causes brain damage. Python is exactly like Java
> > (with the exception of having no primitive types).
> >
> > These notions should state *what* is being passed. In Pascal,
> > that's "value of a slot" or "reference to a slot". To be precise,
> > in Python it is *always* "value of a reference", (think "pointer
> > value", not "value of that which is pointed to").
>
> <sigh> Let's go back to CS101 and start with at least a MINIMAL set of
> common words, definitions and concepts.
>
> 1. Computers contain and operate upon REPRESENTATIONS only.
Nah. They operate on voltage levels represented by 1s and 0s.
The fact that higher levels of representation are built on top of
that is a useful fantasy.
> That is to say, there is no such thing as the "integer number five" in a
> computer, only that which we CHOOSE to REPRESENT the VALUE of the purely
> math-theoretical notion of "5".
See how you take the fantasy for reality? No way does any
computer ever manipulate or represent an integer. The closest
you'll come is an element of a finite algebraic group. But
computer languages let us believe they are integers, for awhile
at least.
> def Nuke(w):
> print w,
> w=None
> print w
>
> Let's call each of these functions:
>
> Nuke(a)
> Nuke(b)
> Nuke(c)
> Nuke(d)
>
> Look at the output:
>
> 5 None
> 5 None
> [5] None
> {"five":5} None
>
> And then look at the original values:
>
> print a,b,c,d
>
> 5 5 [5] {"five":5}
>
> None changed! Therefore, representations the functions modified were
> DIFFERENT from those passed, though it is clear that the correct VALUE
> was passed.
And in each case the VALUE OF THE REFERENCE was
nuked. You didn't modify any referent. You reassigned.
> This behavior meets the classical definition of "Pass By Value".
Yes, the value of the reference. When you say "a=5" in
Python, a holds a reference to an object. The object holds a
representation of something we take to be the number 5.
Passing a to a function is done by copying the value of a (not
the object a references). Therefor assigning to a in the called
function has no effect on the caller. If the object is mutable,
changes to the object will be visible in the caller.
This is NOT unique to Python. Technically, this is "call by
value", but it's "value of the reference". However, because
changes to mutable objects are visible to the caller, people
often call it "call by reference", which, as you've observed, it's
not.
The real problem is that "call by value" and "call by reference"
have an unspoken assumption that variables are slots.
> What other tests have I omitted, and what do they yield?
You've missed a level of indirection. The label is not the object.
- Gordon
More information about the Python-list
mailing list