indirect references

Thomas Wouters thomas at xs4all.net
Mon Jun 5 19:21:19 EDT 2000


On Mon, Jun 05, 2000 at 04:13:30PM -0700, Sean 'Shaleh' Perry wrote:

> > I don't think I am sticking my foot in my mouth: Strings are immutable in
> > Python and passed/assigned by value (not by reference, which is what you are
> > asking for here). Arrays and dictionaries are passed by reference. Maybe you
> > could setup you data to use an array, pass a reference to it and change its
> > value, like this:

> I knew that a string itself was immutable, I guess I was looking for a
> reference object of some kind (a la C pointers, C++/perl references).

Well, if you wanted a reference, you got it :) 

foo = "string"
bar = foo

Makes foo and bar refer to the same object, the string "string". But bar
doesn't point to foo, it merely points to the same object foo points to, so
if you change foo to point somewhere else, foo and bar wont be the same
anymore.

However, if you change the object foo points to, the object bar points to
will change, too -- because it's the same object. But since strings are
immutable, you *can't* change the object foo or bar points to. The only
thing you can do is make foo or bar point to a different string, and you
can't do that for both foo and bar, magically, at the same time.

-- 
Thomas Wouters <thomas at xs4all.net>

Hi! I'm a .signature virus! copy me into your .signature file to help me spread!




More information about the Python-list mailing list