I am working on a small project where I have to use method overloading for mul method. But that mul method has a different arguments.
Method can take 2 argument 1st is self and 2nd is either Instance or integer. So when Method take 2nd argument as Instance it gives me an error. Here is my code
import math
class Vector:
def __init__(self, x, y):
self.x = x
self.y = y
def __mul__(self,v1):
x = self.x * v1.x
y = self.y * v1.y
return Vector(x,y)
def __mul__(self,n):
x = self.x*n
y = self.y*n
return Vector(x,y)
v1 = getVector()
v2 = getVector()
v3 = v1 * v2
v4 = 1
v5 = getVector()
v6 = v4 * v5
So when I am trying to run the program I am getting error that
File "Al.py", line 102, in <module>
main()
File "Al.py", line 88, in main
mul()
File "Al.py", line 47, in mul
v3 = v1 * v2
File "Al.py", line 21, in __mul__
x = self.x*n
Can someone help me to understand this? How can I call the overloaded method without making any changes to my method signature?
__mul__, you wiped out the first version.__mul__()method would need to explicitly check the number and type of arguments it was passed to determine what to do.