鱼C论坛

 找回密码
 立即注册
查看: 2222|回复: 3

[已解决]一个修饰符的问题

[复制链接]
发表于 2016-2-23 13:44:07 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
import time as t

class MyTimer:
    def __init__(self, func, number=10):
        self.prompt = "未开始计时!"
        self.lasted = 0.0
        self.default_timer = t.perf_counter
        self.func = func
        self.number = number
   
    def __str__(self):
        return self.prompt

    __repr__ = __str__


    def __add__(self, other):
        result = self.lasted + other.lasted
        prompt = "总共运行了 %0.2f 秒" % result
        return prompt

    # 内部方法,计算运行时间
    def timing(self):
        
        self.begin = self.default_timer()
        for i in range(self.number):
            self.func()
        self.end = self.default_timer()
        self.lasted = self.end - self.begin
        self.prompt = "总共运行了 %0.2f 秒" % self.lasted
        
    # 设置计时器(time.perf_counter() 或 time.process_time())
    def set_timer(self, timer):
        if timer == 'process_time':
            self.default_timer = t.process_time
        elif timer == 'perf_counter':
            self.default_timer = t.perf_counter
        else:
            print("输入无效,请输入 perf_counter 或 process_time")
@MyTimer
def fun():
    for i in range(10):
        print(i,end='')

print(fun())
------------------------------------------------
Traceback (most recent call last):
  File "D:\新建文本文档.py", line 70, in <module>
    print(f())
TypeError: 'MyTimer' object is not callable

为什么会出现不能运行,我是照着小甲鱼的扩展阅读(修饰符)来打的。谢谢!
最佳答案
2016-2-23 18:39:05
通常提示 is not callable , 去掉括号再试试
  1. >>> fun
  2. 未开始计时!
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2016-2-23 18:39:05 | 显示全部楼层    本楼为最佳答案   
通常提示 is not callable , 去掉括号再试试
  1. >>> fun
  2. 未开始计时!
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-2-23 22:25:46 | 显示全部楼层
hldh214 发表于 2016-2-23 18:39
通常提示 is not callable , 去掉括号再试试

去掉括号,貌似只是运行了__init__方法。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-2-23 23:17:54 | 显示全部楼层
吕浩 发表于 2016-2-23 22:25
去掉括号,貌似只是运行了__init__方法。

相当于实例化了这个类, 当然调用了构造函数了,
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-2-19 20:40

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表