""" Exercise 9.6 from "A primer on..." Create a subclass of the class to represent a point using polar coordinates. """ import numpy as np class Point: def __init__(self, x, y): self.x, self.y = x, y def __str__(self): return f'({self.x}, {self.y})' class PolarPoint(Point): def __init__(self, r, theta): self.r = r self.theta = theta x = r * np.cos(theta) y = r * np.sin(theta) super().__init__(x, y) def __str__(self): return super().__str__() + f', ({self.r}, {self.theta})' def test_PolarPoint(): #create one instance of the class and # verify that all attributes have expected values: tol = 1e-8 pp = PolarPoint(1, np.pi) #expected values: x, y, r, theta = -1, 0, 1, np.pi max_error = max(abs(pp.x - x), abs(pp.y - y), abs(pp.r - r), abs(pp.theta - theta)) assert max_error < tol if __name__ == "__main__": #call the test function test_PolarPoint() #example code to demonstrate class use: p = PolarPoint(1, np.pi) print(p)