Why does changing the str method affect other methods? It seems like changing the __str__method also changes the other methods that return a Point2d object.
import math
class Point2d(object):
def __init__( self, x0, y0 ):
self.x = x0
self.y = y0
def magnitude(self):
return math.sqrt(self.x**2 + self.y**2)
# 2.1
def __str__(self):
#return str((self.x, self.y))
return 'self.x, self.y'
# 2.2
def __sub__(self, other):
return Point2d(self.x - other.x, self.y - other.y)
# 2.4
def __eq__(self, other):
if self.x == other.x and self.y == other.y:
return True
p = Point2d(0,4)
q = Point2d(5,10)
r = Point2d(0,4)
leng = Point2d.magnitude(q)
print("Magnitude {:.2f}".format( leng ))
print(p.__str__(), type(p.__str__())) # 2.1
print(p-q) # 2.2
print(Point2d.__sub__(p,q)) # 2.2 same as line above (line 60).
print(p.__eq__(r))
Expected results:
Magnitude 11.18
self.x, self.y <class 'str'>
(-5, -6)
(-5, -6)
True
Actual results:
Magnitude 11.18
self.x, self.y <class 'str'>
self.x, self.y
self.x, self.y
True