|
|
|
|
|
by ben-schaaf
3849 days ago
|
|
The assignment aspect of __iadd__ is done like any other assignment in python. The one to one equivalent of eliteraspberrie's would be: class Vector2D:
def __iadd__(self, other)
return self + other
No "self mutation" as you suggest. a += b is equivalent to a = a.__iadd__(b) |
|
Your example would be the implementation for immutable Vector2Ds, and is implemented for you if you just wrote __add__. (I presume object.__iadd__ falls back to self.__add__.)
The only time you should actually override __iadd__ is for mutable objects, in which case it should look more like
That's the self mutation I was talking about.Well, strictly that's not OK either, since operators shouldn't be duck-typed. Instead you should do