Using pointers

Aahz aahz at pythoncraft.com
Sat Jul 20 10:20:35 EDT 2002


In article <4210d7a2.0207200538.44a91c0f at posting.google.com>,
Joe Krahn <jkrahn at nc.rr.com> wrote:
>
>Can I make a pointer to an object in Python? I know that all
>objects/variables are references, but not in the same sense as in C.
>How do I get multiple pointers to reference and modify one variable?

No, objects are not references, they're data.  Variables (which I prefer
to call "names" because of the confusion) only contain references, but
it's all implicit.  Thus:

>>> x = []
>>> y = x
>>> y.append('foo')
>>> x
['foo']

This sequence creates the name x, binding it to an empty list object.
Then the name y is created, referring to the same object as x.  y gets
mutated, and because x is bound to the same object, you can see the
result of the mutation by accessing x.
-- 
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

Project Vote Smart: http://www.vote-smart.org/



More information about the Python-list mailing list