Passing by reference

John Machin sjmachin at lexicon.net
Thu Dec 20 15:00:23 EST 2007


On Dec 21, 5:57 am, MartinRineh... at gmail.com wrote:
> Is the following correct?
>
> x = "some string"
>
> x is a reference to "some string"
>
> foo(x)
>
> Reference is passed to function.
>
> In foo:
>     x += " change"
>
> Strings are immutable, so x in foo() now points to a different string
> than x outside foo().
> Right?
>
> Back outside foo.
>
> x = ["some string"]
>
> x is a reference to a list whose first element is a reference to a
> string.
>
> foo(x)
>
> Within foo:
>
> x[0] += " other"
>
> Another string is created, the first element of x is modified to point

Somewhat colloquial/abbreviated. x is a reference. It does not have
elements. You mean "... the first element of the list to which x
refers is modified ...".

> to the new string and back outside foo(), x[0] will point to the new
> string.
>
> Right?

Close enough.




More information about the Python-list mailing list