class Vector(list): def __init__(self, x=0, y=0): list.__init__(self, (x, y)) def __getattr__(self, name): if name == "x": return self[0] elif name == "y": return self[1] def __setattr__(self, name, value): if name == "x": self[0] = value elif name == "y": self[1] = value else: list.__setattr__(self, name, value) def __add__(self, other): return Vector(self.x + other[0], self.y + other[1]) __radd__ = __add__ def __iadd__(self, other): self.x += other[0] self.y += other[1] return self def __sub__(self, other): return Vector(self.x - other[0], self.y - other[1]) def __rsub__(self, other): return Vector(other[0] - self.x, other[1] - self.y) def __isub__(self, other): self.x -= other[0] self.y -= other[1] return self def __mul__(self, other): return Vector(self.x * other, self.y * other) __rmul__ = __mul__ def __imul__(self, other): self.x *= other self.y *= other return self def apply_to_components(self, function): self.x = function(self.x) self.y = function(self.y) def place(self, x=None, y=None): if x is not None: self.x = x if y is not None: self.y = y def move(self, dx=0, dy=0): if dx: self.x += dx if dy: self.y += dy def place_at_origin(self): self.x = 0 self.y = 0