how to pass by reference??

Aahz Maruch aahz at panix.com
Wed Feb 6 20:18:53 EST 2002


In article <jAe88.87233$KM2.2811767 at atlpnn01.usenetserver.com>,
Steve Holden <sholden at holdenweb.com> wrote:
>"Spencer Doidge" <spencer at spencerdoidge.com> wrote ...
>>
>> I really tried to RTM first this time, but I failed.
>> Can someone give me a hint where in the doc html's it explains how to pass
>> by reference?
>
>The same place it explains how to use arguments :-) -- by reference is the
>only way to do it.
>
>References to immutable objects, however, (strings, integers, etc.)
>cannot be used to change the value, since the function argument is a
>*local copy* of the references passed in. Binding that local copy to a
>new value will have no effect on the argument that was passed in to the
>function.

Um, that's not IMO/IME a particularly useful way to look at it.  I don't
remember who first proposed it, but I find talking about "names" and
"bindings" to be much clearer.  When talking about parameter passing, one
would say that the function parameters create new names that get bound to
the passed in objects.  Any time one uses assignment on the function
parameter names (or any other name in Python, for that matter), the name
gets rebound to a different object.

If the object is mutable and mutated, all names that are bound to that
object see the changes.  Mutation, by definition, *never* involves a
rebinding of the object being mutated (that is, mutation never takes
place in an assignment -- though of course the assignment expression
might involve mutation as a side-effect).

(Yes, you're talking about binding, but you don't go whole hog, which
confuses the issue.)

>--
>Consulting, training, speaking: http://www.holdenweb.com/
>Author, Python Web Programming: http://pydish.holdenweb.com/pwp/
>
>"This is Python.  We don't care much about theory, except where it
>intersects with useful practice."  Aahz Maruch on c.l.py

While I'm pleased to be quoted, I do prefer being asked for permission.
Most people have that preference, IME.
-- 
                      --- Aahz  <*>  (Copyright 2002 by aahz at pobox.com)

Hugs and backrubs -- I break Rule 6                 http://www.rahul.net/aahz/
Androgynous poly kinky vanilla queer het Pythonista

"We should forget about small efficiencies, about 97% of the time.
Premature optimization is the root of all evil."  --Knuth



More information about the Python-list mailing list