- Code: Select all
`class Vector(object):`

def __init__(self, x, y, z):

self.x = x

self.y = -y

self.z = z

def __mul__(self, other):

''' Elementwise vector multiplication '''

return Vector(self.x * other.x, self.y * other.y, self.z * other.z)

def __rmul__(self, other):

''' Scalar multiplication '''

return Vector(self.x * other, self.y * other, self.z * other)

Now I need to add a Matrix class that requires matrix-matrix and matrix-vector multiplication support. Is this possible with operator overloading? Would it be acceptable to do something like

- Code: Select all
`class Matrix(object):`

def __init__(self, x1, y1, z1, x2, y2, z2, x3, y3, z3):

...

def __mul__(self, other):

if isinstance(other, Vector):

...

elif isinstance(other, Matrix):

...