python学习算术运算

运算重新定义

算数运算符
__add__(self, other) 定义加法的行为:+
__sub__(self, other) 定义减法的行为:-
__mul__(self, other) 定义乘法的行为:*
__truediv__(self, other) 定义真除法的行为:/
__floordiv__(self, other) 定义整数除法的行为://
__mod__(self, other) 定义取模算法的行为:%
__divmod__(self, other) 定义当被 divmod() 调用时的行为
__pow__(self, other[, modulo]) 定义当被 power() 调用或 ** 运算时的行为
__lshift__(self, other) 定义按位左移位的行为:<<
__rshift__(self, other) 定义按位右移位的行为:>>
__and__(self, other) 定义按位与操作的行为:&
__xor__(self, other) 定义按位异或操作的行为:^
__or__(self, other) 定义按位或操作的行为:|

更多 https://fishc.com.cn/forum.php?mod=viewthread&tid=48793

 1 >>> class New_int(int):
 2     def __add__(self, other):
 3         return int.__sub__(self, other)
 4     def __sub__(self, other):
 5         return int.__add__(self, other)
 6 
 7     
 8 >>> a = New_int(3)
 9 >>> b = New_int(5)
10 >>> a + b
11 -2
12 >>> a - b
13 8
14 
15 
16 >>> class Try_int(int):
17     def __add__(self, other):
18         return int(self) - int(other)
19     def __sub__(self, other):
20         return int(self) + int(other)
21 
22     
23 >>> a = Try_int(3)
24 >>> b = Try_int(5)
25 >>> a+b
26 -2

反运算

当左操作数不支持相应的操作时被调用

 1 >>> class Nint(int):
 2     def __radd__(self, other):
 3         return int.__sub__(self, other)
 4 
 5     
 6 >>> a = Nint(5)
 7 >>> b = Nint(3)
 8 >>> a+b
 9 8
10 >>> 1+b
11 2

执行1+b时,由于1与b的加法不存在,则会反调用b的加法变成b+1的形式执行__add__

故如果需要使结果不变应该调换return里的self和other的位置

>>> class Nint(int):
    def __radd__(self, other):
        return int.__sub__(other, self)

    
>>> a = Nint(5)
>>> b = Nint(3)
>>> a+b
8
>>> 1+b
-2

简单定制计时器

  实现直接输出字符的方法

>>> class A():
    def __repr__(self):
        return "abc"

    
>>> a = A()
>>> a
abc
import time as t

class MyTimer():
    def __init__(self):
        self.unit = ['', '', '', '小时', '分钟', '']
        self.prompt = "未开始计时!"
        self.lasted = []
        self.start0 = 0
        self.stop0 = 0

    def __str__(self):
        return self.prompt

    __repr__ = __str__


    def __add__(self, other):
        prompt = "总共运行了"
        result = []
        for index in range(6):
            result.append(self.lasted[index] + other.lasted[index])
            if result[index]:
                prompt += (str(result[index]) + self.unit[index])
        return prompt
    
    #开始计时
    def start(self):
        self.start0 = t.localtime()
        self.prompt = "请先停止计时"
        print("计时开始...")

    #停止计时
    def stop(self):
        if not self.start0:
            print("请先开始计时")
        else:
            self.stop0 = t.localtime()
            self._calc()
            print("计时结束!")

    #内部方法,计算运行时间
    def _calc(self):
        self.lasted = []
        self.prompt = "总共运行了"
        for index in range(6):
            self.lasted.append(self.stop0[index] - self.start0[index] )
            if self.lasted[index]:
                self.prompt += (str(self.lasted[index]) + self.unit[index])
        print(self.prompt)
        #下一轮初始化
        self.start0 = 0
        self.stop0 = 0
原文地址:https://www.cnblogs.com/jdzhang1995/p/10492466.html