Don't use __getattr__
or __setattr__
for this. Properties are a better fit for your task:
# Inherit from object. This is important.
class Vec2(object):
def __init__(self):
self.data = [0.0, 0.0]
def __getitem__(self, index):
return self.data[index]
def __setitem__(self, index, val):
self.data[index] = val
@property
def x(self):
return self.data[0]
@x.setter
def x(self, val):
self.data[0] = val
@property
def y(self):
return self.data[1]
@y.setter
def y(self, val):
self.data[1] = val
The problem with your __setattr__
attempts is that __setattr__
is called for all attribute assignments, including self.data
, self.mapping
, and any assignments you attempt in __setattr__
itself. You can fix that by delegating to object.__setattr__
for attributes other than x
or y
, but properties are easier. Properties also save you from having to use object.__getattribute__
for attribute access in __getattr__
, which was one of the factors contributing to your infinite recursion problems.
In contrast to __setattr__
, properties only trigger when you try to access the attribute they control. An x
property doesn't have to deal with accesses to attributes other than x
, and you don't have to use alternative attribute access mechanisms when writing a property.