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
def __init__(self, value):
self.value = value
I'm not sure exactly what you are trying to do, but you could do something like:
class FooWrapper(object): def __init__(self, value): self.value = value def __repr__(self): return 'FooWrapper(' + repr(self.value) + ')' def __str__(self): return str(self.value) def __call__(self,value): self.value = value
Here I got rid of your idea of using
__repr__ to hide
FooWrapper since I think it a bad idea to hide from the programmer what is happening at the REPL. Instead -- I used
__str__ so that when you print the object you will print the wrapped value. The
__call__ functions as a default method, which doesn't change the meaning of
= but is sort of what you want:
>>> vals = [1,2,3] >>> vals = FooWrapper("Bob") >>> vals [1, FooWrapper('Bob'), 3] >>> for x in vals: print(x) 1 Bob 3 >>> this = vals >>> this(10) >>> vals [1, FooWrapper(10), 3]
However, I think it misleading to refer to
this as a pointer. It is just a wrapper object, and is almost certain to make dealing with the wrapped object inconvenient.
On Edit: The following is more of a pointer to a list. It allows you to create something like a pointer object with
__call__ used to dereference the pointer (when no argument is passed to it) or to mutate the list (when a value is passed to
__call__). It also implements a form of
p++ called (
pp) with wrap-around (though the wrap-around part could of course be dropped):
class ListPointer(object): def __init__(self, myList,i=0): self.myList = myList self.i = i % len(self.myList) def __repr__(self): return 'ListPointer(' + repr(self.myList) + ',' + str(self.i) + ')' def __str__(self): return str(self.myList[self.i]) def __call__(self,*value): if len(value) == 0: return self.myList[self.i] else: self.myList[self.i] = value def pp(self): self.i = (self.i + 1) % len(self.myList)
Used like this:
>>> vals = ['a','b','c'] >>> this = ListPointer(vals) >>> this() 'a' >>> this('d') >>> vals ['d', 'b', 'c'] >>> this.pp() >>> this() 'b' >>> print(this) b
I think that this is a more transparent way of getting something which acts like a list pointer. It doesn't require the thing pointed to to be wrapped in anything.