Passing by reference

Ben Finney bignose+hates-spam at benfinney.id.au
Thu Dec 20 14:56:46 EST 2007


MartinRinehart at gmail.com writes:

> Is the following correct?
> 
> [lots of references to "references"]

All good so far.

> x[0] += " other"
> 
> Another string is created, the first element of x is modified to point
> to the new string and back outside foo(), x[0] will point to the new
> string.

Change these to talk about "references" again and it'll be true also:

    "Another string is created, the first element of x now refers to
    the new string and back outside foo(), x is still a reference to
    the same list (so its first element is a reference to the same
    string)."

> Right?

Right. In Python, all names, and all elements of container objects,
are references to the corresponding objects. Python has no concept of
"pointers" in the style of C-like languages.

-- 
 \        "I fly Air Bizarre. You buy a combination one-way round-trip |
  `\    ticket. Leave any Monday, and they bring you back the previous |
_o__)  Friday. That way you still have the weekend."  -- Steven Wright |
Ben Finney



More information about the Python-list mailing list