I want to overload the multiplication operator of a vector. At the same time, you can calculate the vector vector, and the vector float
v is the vector
def __mul__(v1, v2):
return vec3(v1.e[0]*v2.e[0],v1.e[1]*v2.e[1],v1.e[2]*v2.e[2])
def __mul__(v, t):
return vec3(t*v.e[0],t*v.e[1],t*v.e[2])
there is obviously a problem with this way of writing. Is there any good solution?
..
add
thanks to Chen Li for his answer. According to the idea in stackoverflow, the code has been modified as follows.
def __mul__(v, par):
if(isinstance(par,vec3)):
return vec3(v.e[0] * par.e[0], v.e[1] * par.e[1], v.e[2] * par.e[2])
else:
return vec3(par*v.e[0],par*v.e[1],par*v.e[2])
I wonder if there are any other ideas? Is there a way to implement similar overloading functions directly from the parameters?