pointers & references in python?
Bjarke Dahl Ebert
bebert at worldonline.dk
Fri Aug 16 10:00:36 EDT 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
>
> OUTPUT:
> 3
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 = [1]
b = a
b[0] = 3
print a[0]
Using an object:
class MyObj:
def __init__(self, val): self.val = val
a = MyIntObj(1)
b = a
b.val = 3
print a.val
Kind regards,
Bjarke
More information about the Python-list
mailing list