Passing by reference
Terry Reedy
tjreedy at udel.edu
Thu Dec 20 17:05:04 EST 2007
<MartinRinehart at gmail.com> wrote in message
news:34cade84-6f3d-46ad-a39d-377235a56689 at x69g2000hsx.googlegroups.com...
| Is the following correct?
|
| x = "some string"
|
| x is a reference to "some string"
x is a name bound to a string object with value 'some string'.
Some people find is useful to call that a 'reference', as you seem to have.
Others get confused by that viewpoint. It depend on exactly what one means
by 'reference'.
| foo(x)
|
| Reference is passed to function.
The first parameter name of foo gets bound to the object referred to by
'x'.
Calling that 'passing by reference' sometimes misleads people as to how
Python behaves.
| In foo:
| x += " change"
|
| Strings are immutable, so x in foo() now points to a different string
| than x outside foo().
| Right?
A function local name x has no particular relationship to a global name
spelled the same, except to confuse things. Best to avoid when possible.
The effect of that statement would be the same outside of the function as
well, pretty much for the reason given. In general, 'y op= x' is the same
as 'y = y op x' except for any side-effects of expression y. Lists are an
exception.
tjr
More information about the Python-list
mailing list