My question comes from this page, while I would like to create a pointer(-like thing) for an list element. The element is a primitive value (string) so I have to create a FooWrapper class as that page says.
I know that by setting __repr__
one can directly access this value.
class FooWrapper(object):
def __init__(self, value):
self.value = value
def __repr__(self):
return repr(self.value)
>>> bar=FooWrapper('ABC')
>>> bar
'ABC'
>>> bar=FooWrapper(3)
>>> bar
3
Now I can use it as an reference of string:
>>> L=[3,5,6,9]
>>> L[1]=FooWrapper('ABC')
>>> L
[3,'ABC',6,9]
>>> this=L[1]
>>> this.value='BCD'
>>> print(L)
[3,'BCD',6,9]
So now I have a pointer-like this
for the list element L[1]
.
However it is still inconvenient since I must use this.value='BCD'
to change its value. While there exists a __repr__
method to make this
directly return this.value
, is there any similar method to make this='BCD'
to do this.value='BCD'
? I know this changes the rule of binding.. but anyway, is it possible?
I would also appreciate if there is a better solution for a list element pointer.
Thank you in advance:)