Question: using python 3: Make the magic method polymorphic. If the val parameter is a number, the method will operate the way it currently does. If
using python 3:
Make the magic method polymorphic. If the val parameter is a number, the method will operate the way it currently does.
If the val parameter is a Point object, the method will return the dot product of the two vectors. To calculate the dot product, multiply the two x attributes and multiply the two y attributes. Return the sum of those two products. (You can do a web search on dot product if you want further information.)
Do not remove your previous tests. But, add another unit test (using the * operator) for the dot product.
class Point: def __init__(self, initX, initY): self.__x = initX self.__y = initY
@property def x(self): return self.__x
@property def y(self): return self.__y
def __mul__(self, val): """ Return a new point that is self multiplied by val """ return Point(self.__x * val, self.__y * val)
if __name__ == "__main__": import test
a = Point(7, -3) b = a * 2 test.testEqual(b.x, 14) test.testEqual(b.y, -6)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
