Python魔法方法(26):__mul__(self, other)方法

27 篇文章 1 订阅
27 篇文章 2 订阅

Python 的对象天生拥有一些神奇的方法,它们总被双下划线所包围,它们是面向对象的 Python 的一切。它们是可以给你的类增加魔力的特殊方法,如果你的对象实现(重载)了某一个魔法方法,那么这个方法就会在特殊的情况下自动被 Python 所调用。

功能

定义乘法的行为。

参数

self 表示乘号左边的对象,other 表示乘号右边的对象。

返回值

一般返回一个对象或一个值。

示例

class MulTest(object):
    def __init__(self, age):
        self.age = age

    def __mul__(self, other):
        print('call mul magic function')
        return MulTest(self.age * other.age)


num_1 = MulTest(12)
num_2 = MulTest(16)
print(f'mul result is:{(num_1 * num_2).age}')

执行示例代码,得到输出结果如下:

call mul magic function
mul result is:192
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,你可以参考下面的代码来修改Fraction的构造方法并实现所需的算术和关系运算: ```python class Fraction: def __init__(self, numerator, denominator): if not isinstance(numerator, int) or not isinstance(denominator, int): raise ValueError("分子和分母必须为整数") if denominator == 0: raise ValueError("分母不能为零") self.numerator = numerator self.denominator = denominator def simplify(self): gcd = self.gcd(self.numerator, self.denominator) self.numerator //= gcd self.denominator //= gcd @staticmethod def gcd(a, b): while b != 0: a, b = b, a % b return a def __str__(self): return f"{self.numerator}/{self.denominator}" def __sub__(self, other): new_numerator = self.numerator * other.denominator - other.numerator * self.denominator new_denominator = self.denominator * other.denominator result = Fraction(new_numerator, new_denominator) result.simplify() return result def __mul__(self, other): new_numerator = self.numerator * other.numerator new_denominator = self.denominator * other.denominator result = Fraction(new_numerator, new_denominator) result.simplify() return result def __truediv__(self, other): new_numerator = self.numerator * other.denominator new_denominator = self.denominator * other.numerator result = Fraction(new_numerator, new_denominator) result.simplify() return result def __gt__(self, other): return self.numerator * other.denominator > other.numerator * self.denominator def __ge__(self, other): return self.numerator * other.denominator >= other.numerator * self.denominator def __lt__(self, other): return self.numerator * other.denominator < other.numerator * self.denominator def __le__(self, other): return self.numerator * other.denominator <= other.numerator * self.denominator def __ne__(self, other): return self.numerator * other.denominator != other.numerator * self.denominator # 测试代码 f1 = Fraction(1, 2) f2 = Fraction(3, 4) print(f1 - f2) print(f1 * f2) print(f1 / f2) print(f1 > f2) print(f1 >= f2) print(f1 < f2) print(f1 <= f2) print(f1 != f2) ``` 这个代码修改了构造方法,确保分子和分母为整数,如果不是整数则抛出异常。还实现了`__sub__`、`__mul__`、`__truediv__`等算术运算符重载方法,以及`__gt__`、`__ge__`、`__lt__`、`__le__`和`__ne__`等关系运算符重载方法

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值