pointers & references in python?
Bjarke Dahl Ebert
bebert at worldonline.dk
Fri Aug 16 16:00:36 CEST 2002
My "//" comments inserted:
> is there something like this in python:
> a = 1 // a refers to 1
> b = a // (removed '&') b refers to the same 1 object
> b = 3 // Now, b refers to another object, 3.
> //You would need b.set(3) instead, but that is not possible.
> print a
In Python, integers are immutable, so even though you can refer to them
(with "b = a", b refers to the same object as a) you cannot use that
reference to change it (there is no such thing as b.set(3)).
Your 'a' has to be a member of something, typically attribute of an object
or entry in a list.
Using a list:
a = 
b = a
b = 3
Using an object:
def __init__(self, val): self.val = val
a = MyIntObj(1)
b = a
b.val = 3
More information about the Python-list