Using pointers
Jim Jinkins
j-jinkins at usa.net
Sat Jul 20 13:27:02 EDT 2002
Joe Krahn 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?
>
>
You can't. This is a 'definition of terms issue.
Each variable _is_ a pointer. You can have multiple variables pointing
to the same object.
If the object is mutable, changes made by accessing it through any of
the variables are seen when accessing it through any of the others.
a = 5
b = 5
c = 5
a = 6 # b and c still reference 5.
print "%s %s %s" % (a, b, c)
6 5 5
a = []
b = a
c = b
a.append[1]
c.append[3]
print "%s %s %s" % (a, b, c)
[1, 3] [1, 3] [1, 3]
When I first came to dynamically typed languages from C and COBOL, this
was really hard to wrap my head around. Try Diving Into Python which is
linked from the Python Language website.
Jim Jinkins
More information about the Python-list
mailing list