__add__ but not __iadd__. A second name points to the first object before += is used.
class Vec:
def __init__(self, x, y):
self.x, self.y = x, y
def __add__(self, o):
return Vec(self.x + o.x, self.y + o.y)
def __str__(self):
return f"({self.x}, {self.y})"
a = Vec(1, 2)
b = a
a += Vec(3, 4)
print(a, b, a is b)
What does the program print?Log in to join the discussion.
Loading…