如何用python编写一个计时器的程序

python计时器的程序的代码和注释
import time as t
#引入time模块
class MyTimer():
    def __init__(self):
#构造函数
        self.unit = ['年','月','日','时','分','秒']
#设置时间单位
#设置一系列初始属性
        self.prmpt = "未开始计时"
        self.lasted = []
        self.begin = 0
        self.end = 0
 
    def __str__(self):
#魔术方法,当直接在外部调用print()时,执行此方法
        return self.prmpt
 
    __repr__ = __str__
#当外部生成对象后,如 
#a = class_name()
#a
#操作后,执行此代码
    #开始计时
    def start(self):
        self.begin = t.localtime()
        print('计时开始')
 
    #停止计时
    def stop(self):
        if not self.begin:
            print("提示,请先调用start()进行计时")
        else:
            self.end = t.localtime()
            self._calc()
            print('计时结束')
 
    def __add__(self,other):
#此方法用来计算两次计时结果的和
        prmpt = "总共运行了"
        result = []
        for index in range(6):
            result.append(self.lasted[index] + other.lasted[index])
#给result list添加数值
            if result[index]:
                prmpt += (str(result[index]) + self.unit[index])
        return prmpt
 
    #内部方法,计算运行时间
    def _calc(self):
        
        self.prmpt = "总共运行了"
        for index in range(6):
            self.lasted.append(self.end[index] - self.begin[index])
            if self.lasted[index]:
                self.prmpt += (str(self.lasted[index]) + self.unit[index])
            
        #为下一轮计时初始化变量
        self.begin = 0
        self.end = 0
原文地址:https://www.cnblogs.com/tan2810/p/11236267.html