第042讲: 魔法方法:算术运算1 | 学习记录(小甲鱼零基础入门学习Python)

(标答出处: 鱼C论坛)
《零基础入门学习Python》

测试题:
在这里插入图片描述
答:工厂函数其实就是一个类对象,当你调用他们的时候实际上就是创建了一个对象。 例:

a = int ('123')
print (a)
b = int ('345')
print (b)
print (a+b)

运行结果
在这里插入图片描述
在这里插入图片描述
答:当实例对象进行假发操作时,会自动调用 __ add __ 的魔法方法。
在这里插入图片描述
答 :类的属性名和方法名绝对不能相同。 如果如同下面这样写,那么问题就出现了。

class Foo :
    def __init__ (self) :
        self.foo = 'I love you'
    def foo(self) :
        return self.foo
    
foo = Foo() 
foo.foo()

运行结果:
在这里插入图片描述

在这里插入图片描述

运算符对应的魔法方法
+__ add __(self,other)
-__ sub __(self,other)
*__ mul __(self,other)
/__ truediv__(self,other)
//__ floordiv__(self,other)
%__ mod __(self,other)
divmod(a,b)divmod(self,other)
**__ pow __(self, other[, modulo])
<<__ lshiift __(self,other)
>>__ rshift __(self,other)
&__ and __(self, other)
^__ xor __(self, other)
I__ or __(self, other)

在这里插入图片描述
答:说明python支持鸭子类型(duck typing)风格。

动动手
在这里插入图片描述

class Nstr (str):
    def __sub__ (self,other):
        return self.replace(other,'')

a = Nstr ('I love Fishc yeyeye')
b = Nstr ('ye')
print (a-b)

运行结果:
在这里插入图片描述
在这里插入图片描述

class Nstr (str):
    def __lshift__ (self,other) :
        return self[other:] +self[:other]
    def __rshift__ (self,other) :
        return self[:-other]+self[-other:]

a = Nstr ('I love Fishc yeyeye!')
print (a<<3)
print (a>>3)

运行结果:
在这里插入图片描述
在这里插入图片描述

class Nstr (str):
    """加"""
    def __add__ (self,other) :
        total1 = 0
        total2 = 0
        for each in self :
            total1 = total1 + ord (each)
        self = total1
        for each in other :
            total2 = total2 + ord (each)
        other = total2
        return self.__add__(other)
    """减"""
    def __sub__ (self,other) :
        total1 = 0
        total2 = 0
        for each in self :
            total1 = total1 + ord (each)
        self = total1
        for each in other :
            total2 = total2 + ord (each)
        other = total2
        return self.__sub__(other)
    """乘"""
    def __mul__ (self,other) :
        total1 = 0
        total2 = 0
        for each in self :
            total1 = total1 + ord (each)
        self = total1
        for each in other :
            total2 = total2 + ord (each)
        other = total2
        return self.__mul__ (other)
    """除"""
    def __truediv__ (self,other) :
        total1 = 0
        total2 = 0
        for each in self :
            total1 = total1 + ord (each)
        self = total1
        for each in other :
            total2 = total2 + ord (each)
        other = total2
        return self.__truediv__(other)
    """整除"""
    def __floordiv__ (self,other) :
        total1 = 0
        total2 = 0
        for each in self :
            total1 = total1 + ord (each)
        self = total1
        for each in other :
            total2 = total2 + ord (each)
        other = total2
        return self.__floordiv__(other)

a = Nstr ('I love Xmy !')
b = Nstr ('love')
print (a+b)
print (a-b)
print (a*b)
print (a/b)
print (a//b)

运行结果:
在这里插入图片描述
或者如下:

class Nstr (str):
    def __init__ (self,arg = '') :
        if isinstance (arg , str ) : 
            self.total = 0
            for each in arg :
                self.total += ord(each)
        else :
            print ('参数错误!')
    """加"""
    def __add__ (self,other) :
        return self.total  + other.total
    """减"""
    def __sub__ (self,other) :
        return self.total  - other.total
    """乘"""
    def __mul__ (self,other) :
        return self.total  * other.total
    """除"""
    def __truediv__ (self,other) :
        return self.total  / other.total
    """整除"""
    def __floordiv__ (self,other) :
        return self.total  // other.total

a = Nstr ('FishC')
b = Nstr ('love')
print (a+b)
print (a-b)
print (a*b)
print (a/b)
print (a//b)
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值