I have a Vector2 class:
class Vector2():
def __init__(self, x=0, y=0):
self.x = x
self.y = y
def __add__(self, other):
return Vector2(self.x + other.x, self.y + other.y)
def __sub__(self, other):
return Vector2(self.x - other.x, self.y - other.y)
def __mul__(self, other):
return Vector2(self.x * other, self.y * other)
def __neg__(self):
return Vector2(-self.x, -self.y)
def magnitude(self):
return math.sqrt(self.x ** 2 + self.y ** 2)
@classmethod
def distance(self, v1, v2):
return math.sqrt((v2.x - v1.x) ** 2 + (v2.y - v1.y) ** 2)
def normalize(self):
return self * (1/self.magnitude())
When I try to do 1.0 * Vector2(), I get the error:
TypeError: unsupported operand type(s) for *: 'float' and 'instance'
However, sometimes it works as intended:
#this works as intended, s is a float
ball.pos -= ball.vel.normalize() * s
ball.vel is a vector and I am able to multiply by a float. Vectors are multiplied by floats in many sections of my code without errors.
Does anyone know where this inconsistency comes from?
Thanks
Vector2() * 1.0and not1.0 * Vector2()?float * whateveris defined byfloat, whereaswhatever * floatis defined bywhatever