I have two Point objects and the code looks like this:
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
a = Point(1, 3)
b = Point(4, 2)
max(a, b) # Make this output Point(4, 3)
My question is: "How can I implement a custom max
function for the Point class that will return Point(max(self.x, other.x), max(self.y, other.y))
?" The max function seems to just look at the __lt__
and return the highest.
def max(self, other): return Point(max(self.x, other.x), max(self.y, other.y))
? – furas Jan 24 '20 at 11:21max(a, b)
can returna
orb
- it can't generate new point with different values. – furas Jan 24 '20 at 11:25max
. one could write a newmax
just like you suggested in your first comment but not tied to thePoint
class. I wouldn't recommend this, though – Marv Jan 24 '20 at 11:29