I'm defining a Sqrt
class to help with some complications in my code, since it's going to be used a lot. The class has a value val
, which is the actual value of the square root being computed.
Code:
# Square root class (helps w accuracy and readability)
class Sqrt(object):
def __init__(self, x: float) -> None:
self.sqr_val = x
self.val = math.sqrt(x)
def __str__(self) -> str:
return f"sqrt({self.sqr_val})"
def __pow__(self, n: float) -> float:
return self.sqr_val ** n / 2
The issue I'm facing is the need to define every dunder method ("magic method") to handle arithmetic operations like +
, -
, *
, /
, etc.:
class Sqrt(object):
def __init__(self, x: float) -> None:
self.sqr_val = x
self.val = math.sqrt(x)
def __str__(self) -> str:
return f"sqrt({self.sqr_val})"
def __pow__(self, n: float) -> float:
return self.sqr_val ** n / 2
def __add__(self, x):
return self.val + x
def __radd__(self, x):
return self.val + x
def __sub__(self, x):
return self.val - x
def __rsub__(self, x):
return x - self.val
def __mul__(self, x):
return self.val * x
def __rmul__(self, x):
return self.val * x
def __truediv__(self, x):
return self.val / x
What I'd like is to be able to define these dunders implicitly, telling Python to use the normal int/float dunders for my class, but using self.val
instead of self
.
I have tried inheriting from int
and using super()
but that doesn't decrease repetition whatsoever.
Is there a way to do this (maybe some fancy descriptor)? Or do I write every applicable dunder out manually?