classVectorND(object): def__init__(self, *args): """Create a VectorND from a sequence of real numbers.""" self.values = list(args)
def__len__(self): """Return the length of the vector.""" returnlen(self.values)
def__eq__(self, other): """Check if the vector is equal to another.""" ifnotisinstance(other, VectorND): returnFalse return self.values == other.values
def__add__(self, other): """Add two VectorND objects and return a new VectorND.""" iflen(self) != len(other): raise ValueError("vector lengths are incompatible") return VectorND(*(a + b for a, b inzip(self.values, other.values)))
def__sub__(self, other): """Subtract one VectorND object from another.""" iflen(self) != len(other): raise ValueError("vector lengths are incompatible") return VectorND(*(a - b for a, b inzip(self.values, other.values)))
def__repr__(self): """Output the "official" string representation of the object.""" returnf"Vector: {self.values}"