The question at
http://stackoverflow.com/questions/1145722/simulating-pointers-in-python
asking how to simulate pointers in Python had a nice suggestion in the solutions, namely to do
class ref:
def __init__(self, obj): self.obj = obj
def get(self): return self.obj
def set(self, obj): self.obj = obj
which can then be used to do e.g.
a = ref(1.22)
b = ref(a)
print a # prints 1.22
print b.get() # prints 1.22
The class can be modified to avoid the use of get
for the print statement by adding
def __str__(self): return self.obj.__str__()
Then,
print b # prints out 1.22
Now I would like to be able to do arithmetic with b in the same way as a, which I guess would be equivelent to saying that I want a
and b
to behave exactly like obj
. Is there anyway to do this? I tried adding methods such as
def __getattribute__(self, attribute): return self.obj.__getattribute__(attribute)
def __call__(self): return self.obj.__call__()
But regardless of this, the output of
print a + b
is always
Traceback (most recent call last):
File "test.py", line 13, in <module>
print a + b
TypeError: unsupported operand type(s) for +: 'instance' and 'instance'
Does anyone have any ideas on how to modify the ref
class to allow this?
Thanks for any advice!