Logo 逆向知识库

运算符重载

class NewInt:
    def __init__(self, val):
        self.val = val

    def __repr__(self):
        return f"NewInt({self.val})"

    def __str__(self):
        return str(self.val)

    def __add__(self, other):
        if isinstance(other, NewInt):
            result = self.val + other.val
        elif isinstance(other, int):
            result = self.val + other
        else:
            return NotImplemented
        print(f"Adding: {self.val} + {other} = {result}")
        return NewInt(result)

    def __radd__(self, other):
        return self.__add__(other)

    def __sub__(self, other):
        if isinstance(other, NewInt):
            result = self.val - other.val
        elif isinstance(other, int):
            result = self.val - other
        else:
            return NotImplemented
        print(f"Subtracting: {self.val} - {other} = {result}")
        return NewInt(result)

    def __rsub__(self, other):
        if isinstance(other, int):
            result = other - self.val
            print(f"Subtracting: {other} - {self.val} = {result}")
            return NewInt(result)
        return NotImplemented

    def __mul__(self, other):
        if isinstance(other, NewInt):
            result = self.val * other.val
        elif isinstance(other, int):
            result = self.val * other
        else:
            return NotImplemented
        print(f"Multiplying: {self.val} * {other} = {result}")
        return NewInt(result)

    def __rmul__(self, other):
        return self.__mul__(other)

    def __truediv__(self, other):
        if isinstance(other, NewInt):
            if other.val == 0:
                raise ZeroDivisionError("Division by zero is undefined.")
            result = self.val / other.val
        elif isinstance(other, int):
            if other == 0:
                raise ZeroDivisionError("Division by zero is undefined.")
            result = self.val / other
        else:
            return NotImplemented
        print(f"Dividing: {self.val} / {other} = {result}")
        return NewInt(result)

    def __rtruediv__(self, other):
        if self.val == 0:
            raise ZeroDivisionError("Division by zero is undefined.")
        if isinstance(other, int):
            result = other / self.val
            print(f"Dividing: {other} / {self.val} = {result}")
            return NewInt(result)
        return NotImplemented

    def __mod__(self, other):
        if isinstance(other, NewInt):
            result = self.val % other.val
        elif isinstance(other, int):
            result = self.val % other
        else:
            return NotImplemented
        print(f"Modulo: {self.val} % {other} = {result}")
        return NewInt(result)

    def __rmod__(self, other):
        if isinstance(other, int):
            result = other % self.val
            print(f"Modulo: {other} % {self.val} = {result}")
            return NewInt(result)
        return NotImplemented

    def __pow__(self, other):
        if isinstance(other, NewInt):
            result = self.val ** other.val
        elif isinstance(other, int):
            result = self.val ** other
        else:
            return NotImplemented
        print(f"Power: {self.val} ** {other} = {result}")
        return NewInt(result)

    def __rpow__(self, other):
        if isinstance(other, int):
            result = other ** self.val
            print(f"Power: {other} ** {self.val} = {result}")
            return NewInt(result)
        return NotImplemented

    def __neg__(self):
        result = -self.val
        print(f"Negating: -{self.val} = {result}")
        return NewInt(result)

    def __abs__(self):
        result = abs(self.val)
        print(f"Absolute: |{self.val}| = {result}")
        return NewInt(result)