how to pass by reference??

Steve Holden sholden at holdenweb.com
Thu Feb 7 09:34:33 EST 2002


"Aahz Maruch" <aahz at panix.com> wrote in message
news:a3sklt$7vr$1 at panix2.panix.com...
> In article <jAe88.87233$KM2.2811767 at atlpnn01.usenetserver.com>,
> Steve Holden <sholden at holdenweb.com> wrote:
> >"Spencer Doidge" <spencer at spencerdoidge.com> wrote ...
> >>
[he wants to pass arguments by reference]
>
> 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.
>
Maybe we should beat this one to death with a rock ;-)  I should point out
this is not for Aahz' benefit, since he knows it already.

Any time one uses assignment, one causes a name to be bound to an object.
Argument passing is actually binding a name (the argument name, present in
the function's local namesspace) to an existing object -- whatever was
passed as an argument, whether it was written as a complex expression (which
could create an object "on the fly") or simply as an existing name (which
would be a simple reference to an existing object).

Assignment to the argument name inside the function, as Aahz points out,
simply rebinds the argument name to a new value. Which means that such an
assignment will not change the object passed as an argument.

Assignment to *a component of the argument*, however, is a completely
different case (as I think someone else has already pointed out). So, if you
pass in to a function an argument arg, the assignment

    arg = something_new

will leave the argument object completely unaltered. However, the assignment

    arg[0] = something_new

will modify a list argument, and this modification will be visible outside
the function as well as inside it. You have mutated the object passed as an
argument, and so all names bound to that object will access the mutated
value. Similarly with arg.attr = something.new, arg.append(something), and
so on.

> 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).
>
You have to be careful to distinguish here between simple assignments (x =
expr) and augmented assignments (x += expr), since the latter are explicitly
allowed to mutate the object referenced by the name, but need not do so if
the implementation of the object concerned chooses not to. The former, I
agree, are simple rebindings.

> (Yes, you're talking about binding, but you don't go whole hog, which
> confuses the issue.)
>
I didn't get where I am today by not going the whole hog <wink>.

luckily-tim's-at-developers'-day-ly y'rs  - steve
--
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







More information about the Python-list mailing list