Pointers

Richard Jones Richard.Jones at fulcrum.com.au
Wed Mar 15 23:44:45 CET 2000


[Curtis Jensen]
> I haven't found anything about pointers in python.  Is there a pointer
> type?  If so, what is the syntax?

   In Python, the things you have come to know as variables are best thought of 
as "labels" for objects. In old-fashioned pointer-language-speak, they're about 
as equivalent to references or pointers as you're going to get.

>>> a = 'hello world'
>>> id(a)
135105304

   In this case, 'a' is a label for the string object 'hello world'. We could 
assign another "pointer" to the same string by:

>>> b = a
>>> id(b)
135105304

   So 'b' is now a label on the same object as 'a'. In pointer-language-speak, 
it's a pointer referencing the same data. Note that Python special-cases some 
very common objects (only immutable ones) so that you're using a reference to 
the same object:

>>> c = 1
>>> id(c)
134955600
>>> d = 1
>>> id(d)
134955600

    Neat huh? Common strings that you use in your programs may be intern()'ed 
... but I'll leave that as an exercise for you to find out about (see the 
Library Reference for details about intern() and id()).



        Richard






More information about the Python-list mailing list