pointer

Ian Bicking ianb at colorstudy.com
Tue Apr 9 15:53:59 EDT 2002


On Tue, 2002-04-09 at 09:45, Henning Peters wrote:
> i would like to store values in two different locations. both values should
> always be equal. the one could be a pointer on the other. is this possible
> to do this in python?

You can simulate explicit pointers with lists (nearly everything in
Python has implicit pointers, which is what other people are pointing
out).  Or, maybe more clearly, you can follow the pointer pattern in
Python without using anything called a pointer.  For instance:

int dosomething(int* x) {
    *x = 10; // <-- changing the slot x, not just the binding of x 
             // for this function
}

def dosomething(x):
    x = 10

The Python version will actually do nothing, since if you do:

a = 5
dosomething(a)
# a is still 5

But you could do:

def dosomething(alist)
    alist[0] = 10

a = [5]
dosomething(a)
# a is now [10]

So [0] is like the * operator, and [something] is like the & operator. 
In other words, a list creates a second pointer.  Objects slots are
similar.

  Ian







More information about the Python-list mailing list