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)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.