Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

using python 3: Change the name of the scale method to be the magic method for m

ID: 3713849 • Letter: U

Question

using python 3:

Change the name of the scale method to be the magic method for multiplication. Use the * operator in the testing section (since there is no longer a method named scale). The tests should still pass.

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 scale(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.scale(2)
test.testEqual(b.x,14)
test.testEqual(b.y,-6)

Explanation / Answer

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)