课时44:魔法方法:简单定制

发布时间:2019-03-03 10:38:16编辑:auto阅读(2026)

    目录:

      一、简单定制

      二、课时44课后习题及答案

     

    ****************

    一、简单定制

    ****************

    基本要求:
    1>> 定制一个计时器的类
    2>> start和stop方法代表启动计时和停止计时
    3>> 假设计时器对象t1,print(t1)和直接调用t1均显示结果
    4>> 当计时器未启动或已经停止计时,调用stop方法会给予温馨的提示
    5>> 两个计时器对象可以进行相加:t1 + t2
    6>> 只能使用提供的有限资源完成

     

    你需要这些资源:

    1>> 使用time模块的localtime方法获取时间
       【扩展阅读】:time 模块详解(时间获取和转换)
    2>> time.localtime返回struct_time的时间格式
    3>> 表现你的类:__str__ ()和 __repr__()魔法方法

    >>> class A:
        def __str__(self):
            return "小甲鱼是帅哥"
    
        
    >>> a = A()
    >>> print(a)
    小甲鱼是帅哥
    >>> a
    <__main__.A object at 0x0000020BB2E537F0>
    >>> class B:
        def __repr__(self):
            return "小甲鱼是帅哥"
    
        
    >>> b = B()
    >>> b
    小甲鱼是帅哥

     

    有了这些知识,可以开始编写代码了:

     

    import time as t
    
    class MyTimer:
        #开始计时
        def start(self):
            self.start = t.localtime()
            print("计时开始...")
        #停止计时
        def stop(self):
            self.stop = t.localtime()
            print("计时结束!")
    
        """
        好,万丈高楼平地起,把地基写好后,应该考虑怎么进行计算了。
        localtime()返回的是一个时间元组的结构,只需要在前边6个元素,
        然后将stop的元素依此减去start对应的元素,将差值存放在一个新的列表里:
        """
        #停止计时
        def stop(self):
            self.stop = t.localtime()
            self._calc()
            print("计时结束!")
    
        
        # 内部方法,计算运行时间
        def _calc(self):
            self.lasted = []
            self.prompt = "总共运行了"
            for index in range(6):
                self.lasted.append(self.stop[index] - self.start[index])
                self.prompt += str(self.lasted[index])
    
            print(self.prompt)
    >>> t1 = MyTimer()
    >>> t1.start()
    计时开始...
    >>> t1.stop()
    总共运行了000008
    计时结束!

    已经基本实现计时功能了,接下来需要完成“print(t1)和直接调用t1均显示结果”,那就要通过重写__str__()和__repr__()魔法方法来实现:

        def __str__(self):
            return self.prompt
        __repr__ = __str__
    >>> t1 = MyTimer()
    >>> t1.start()
    计时开始...
    >>> t1.stop()
    总共运行了000004
    计时结束!
    >>> t1
    总共运行了000004

    似乎做得很不错了,但这里还有一些问题。假使用户不按常理出牌,问题就会很多:

    >>> t1 = MyTimer()
    >>> t1
    Traceback (most recent call last):
      File "<pyshell#10>", line 1, in <module>
        t1
      File "C:\Users\14158\AppData\Local\Programs\Python\Python37\lib\idlelib\rpc.py", line 617, in displayhook
        text = repr(value)
      File "C:\Users\14158\Desktop\lalallalalal.py", line 36, in __str__
        return self.prompt
    AttributeError: 'MyTimer' object has no attribute 'prompt'

    当直接执行t1的时候,Python会调用__str__()魔法方法,但它却说这个类没有prompt属性。prompt属性在哪里定义的?在_calc()方法里定义的,对不?但是没有执行stop()方法,_calc()方法就没有被调用到,所以也就没有prompt属性的定义了。

    要解决这个问题也很简单,大家应该还记得在类里边,用得最多的一个魔法方法是什么?是__init__()嘛,所有属于实例对象的变量只要在这里边先定义,就不会出现这样的问题了。

        def __init__(self):
            self.prompt = "未开始计时!"
            self.lasted = []
            self.start = 0
            self.stop = 0
    >>> t1 = MyTimer()
    >>> t1
    未开始计时!
    >>> t1.start()
    Traceback (most recent call last):
      File "<pyshell#16>", line 1, in <module>
        t1.start()
    TypeError: 'int' object is not callable

    这里又报错了(当然是故意的),先检查一下出现了什么问题?

    Python这里抛出了一个异常:TypeError: 'int' object is not callable

    仔细瞧,在调用start()方法的时候报错,也就是说,Python认为start是一个整型变量,而不是一个方法。为什么呢?大家看__init__()方法里,是不是也命名了一个叫做self.start的变量,如果类中的方法名和属性同名,属性会覆盖方法。

    好了,让我们把self.start和self.stop都改为self.begin和self.end吧!

    现在程序没什么问题了,但显示的时间不怎么好看,希望按章年月日时分秒来显示,所以这里添加一个列表用于存放对应的单位,然后再适当的地方增加温馨提示:

     

    import time as t
    
    class MyTimer:
        def __init__(self):
            self.unit = ['', '', '', '小时', '分钟', '']
            self.prompt = "未开始计时!"
            self.lasted = []
            self.begin = 0
            self.end = 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.begin = t.localtime()
            self.prompt = "提示:请先调用 stop() 停止计时!"
            print("计时开始...")
    
        # 停止计时
        def stop(self):
            if not self.begin:
                print("提示:请先调用 start() 进行计时!")
            else:
                self.end = t.localtime()
                self._calc()
                print("计时结束!")
    
        # 内部方法,计算运行时间
        def _calc(self):
            self.lasted = []
            self.prompt = "总共运行了"
            for index in range(6):
                self.lasted.append(self.end[index] - self.begin[index])
                if self.lasted[index]:
                    self.prompt += (str(self.lasted[index]) + self.unit[index])
            # 为下一轮计时初始化变量
            self.begin = 0
            self.end = 0

     

    最后再写一个魔法方法__add__(),让两个计时器对象相加会自动返回时间的和:

        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
    >>> t1 = MyTimer()
    >>> t1
    未开始计时!
    >>> t1.stop()
    提示:请先调用 start() 进行计时!
    >>> t1.start()
    计时开始...
    >>> t1
    提示:请先调用 stop() 停止计时!
    >>> t1.stop()
    计时结束!
    >>> t1
    总共运行了7秒
    >>> t2 = MyTimer()
    >>> t2.start()
    计时开始...
    >>> t2.stop()
    计时结束!
    >>> t1 + t2
    '总共运行了18秒'

     

    看上去代码不错,也能正常计算了。但是这个程序还有几点不足的地方:

    (1)如果开始计时的时间是(2022年2月22日16:30:30),停止时间是(2025年1月23日15:30:30),那按照我们用停止时间减开始时间的计算方式就会出现负数(3年-1月1天-1小时),你应该对此做一些转换。
    (2)现在的计算机速度都非常快,而我们这个程序最小的计算单位却只是秒,精度是远远不够的。

     

    *******************************

    二、课时44课后习题及答案

    *******************************

     

     

关键字