I have a Python class which has various internal variables that I want to expose as x, y, a simple example being:
class Vec2:
def __init__(self):
self.data = [0.0, 0.0]
self.mapping = {'x':0, 'y':1}
def __getitem__(self, index):
return self.data[index]
def __setitem__(self, index, value):
self.data[index] = value
def __getattr__(self, key):
if key in self.mapping:
return self.data[self.mapping[key]]
else:
raise AttributeError
def _setattr__(self, key, value):
# ?????
pass
I would have thought that this would not work because __getattr__
accesses members of self. However, everything works as long as I don't try to define a __setattr__
- even if I just make it equal to pass
, I get recursion. How should I implement __setattr__
? In particular, what should I be calling in the case that they key is not x or y?
Also, in general my question is whether this the right way of implementing the desired behavior such as:
v = Vec2()
# this
v.x = 1.0
v.y = 0.0
# or this
v[0] = 1.0
v[1] = 0.0
or whether there are better alternatives. I'm using python 2.7.5. Thanks!