Subject: Passing by reference
From: Terry Reedy
Date: 12/20/2007 5:05:04 PM
<MartinRinehart@gmail.com> wrote in message
news:34cade84-6f3d-46ad-a39d-377235a56689@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
Subject: Passing by reference
From: aahz@pythoncraft.com (Aahz)
Date: 12/20/2007 7:54:06 PM
In article <34cade84-6f3d-46ad-a39d-377235a56689@x69g2000hsx.googlegroups.com>,
<MartinRinehart@gmail.com> wrote:
>
>Is the following correct?
Sort-of, but I would say that it's misleadingly correct. Try this:
http://starship.python.net/crew/mwh/hacks/objectthink.html
--
Aahz (aahz@pythoncraft.com) <*> http://www.pythoncraft.com/
"Typing is cheap. Thinking is expensive." --Roy Smith
Subject: Passing by reference
From: Sion Arrowsmith
Date: 12/21/2007 12:32:56 PM
Michael Sparks <ms@cerenity.org> wrote:
>def bar():
> global x
> x[0] += " another"
> print id(x[0])
... and for bonus marks, explain why the "global x" in this function
is not required.
--
\S -- siona@chiark.greenend.org.uk -- http://www.chaos.org.uk/~sion/
"Frankly I have no feelings towards penguins one way or the other"
-- Arthur C. Clarke
her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
|