Skip to content

Commit 9a4b6b0

Browse files
committed
fix: reflective dunder methods operation order
The reflective dunder methods are called when the normal dunder fails. This normally does not matter for commutative operations, like addition, but for division and subtraction, this can lead to errors. For example, def __sub__(self, other) is meant to solve `self - other` while def __rsub__(self, other) is meant to solve `other - self` The following reflective dunder methods were fixed to honor the correct direction of operations: __rsub__ __rfloordiv__ __rtruediv__ __rmod__ __rpow__
1 parent 32a7513 commit 9a4b6b0

File tree

1 file changed

+31
-11
lines changed

1 file changed

+31
-11
lines changed

DynamicVector/DynamicVector.py

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1560,9 +1560,13 @@ def __sub__(self, other): # To get called on subtraction operation using - oper
15601560
res -= other
15611561
return res
15621562

1563-
def __rsub__(self, other): # To get called on subtraction operation using - operator.
1564-
res = self.copy()
1565-
res -= other
1563+
def __rsub__(self, other): # To get called on subtraction operation using other - self operator.
1564+
if isinstance(other, DynamicVector):
1565+
res = other.copy()
1566+
res -= self.view
1567+
else:
1568+
res = DynamicVector.from_values(other)
1569+
res -= self.view
15661570
return res
15671571

15681572
def __mul__(self, other): # To get called on multiplication operation using * operator.
@@ -1581,8 +1585,12 @@ def __floordiv__(self, other): # To get called on floor division operation usin
15811585
return res
15821586

15831587
def __rfloordiv__(self, other): # To get called on floor division operation using // operator.
1584-
res = self.copy()
1585-
res //= other
1588+
if isinstance(other, DynamicVector):
1589+
res = other.copy()
1590+
res //= self.view
1591+
else:
1592+
res = DynamicVector.from_values(other)
1593+
res //= self.view
15861594
return res
15871595

15881596
def __truediv__(self, other): # To get called on division operation using / operator.
@@ -1591,8 +1599,12 @@ def __truediv__(self, other): # To get called on division operation using / ope
15911599
return res
15921600

15931601
def __rtruediv__(self, other): # To get called on division operation using / operator.
1594-
res = self.copy()
1595-
res /= other
1602+
if isinstance(other, DynamicVector):
1603+
res = other.copy()
1604+
res /= self.view
1605+
else:
1606+
res = DynamicVector.from_values(other)
1607+
res /= self.view
15961608
return res
15971609

15981610
def __mod__(self, other): # To get called on modulo operation using % operator.
@@ -1601,8 +1613,12 @@ def __mod__(self, other): # To get called on modulo operation using % operator.
16011613
return res
16021614

16031615
def __rmod__(self, other): # To get called on modulo operation using % operator.
1604-
res = self.copy()
1605-
res %= other
1616+
if isinstance(other, DynamicVector):
1617+
res = other.copy()
1618+
res %= self.view
1619+
else:
1620+
res = DynamicVector.from_values(other)
1621+
res %= self.view
16061622
return res
16071623

16081624
def __pow__(self, other): # To get called on calculating the power using ** operator.
@@ -1611,8 +1627,12 @@ def __pow__(self, other): # To get called on calculating the power using ** ope
16111627
return res
16121628

16131629
def __rpow__(self, other): # To get called on calculating the power using ** operator.
1614-
res = self.copy()
1615-
res **= other
1630+
if isinstance(other, DynamicVector):
1631+
res = other.copy()
1632+
res **= self.view
1633+
else:
1634+
res = DynamicVector.from_values(other)
1635+
res **= self.view
16161636
return res
16171637

16181638
def __lt__(self, other): # To get called on comparison using < operator.

0 commit comments

Comments
 (0)