鱼C论坛

 找回密码
 立即注册
查看: 1173|回复: 5

[已解决]老师这段代码func需要输入什么类型的数据才能运行

[复制链接]
发表于 2022-3-30 15:14:27 | 显示全部楼层 |阅读模式

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

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

x
import time as t

class MyTimer:
    def __init__(self, func, number=1000000):
        self.prompt = "未开始计时!"
        self.lasted = 0.0
        self.default_timer = t.perf_counter
        self.func = 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):
            print(self.func())
            self.func()                                           这个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")
t = MyTimer()
谢谢老师的解答
最佳答案
2022-3-30 15:15:32
传入一个函数呀
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-3-30 15:15:32 | 显示全部楼层    本楼为最佳答案   
传入一个函数呀

评分

参与人数 1荣誉 +5 鱼币 +5 贡献 +3 收起 理由
redaiconglin + 5 + 5 + 3

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-3-30 15:19:26 | 显示全部楼层

我传入了3说是不能传入一个int类型,我传入了字符串也不行。列表也不行
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-3-30 15:20:15 | 显示全部楼层


import time as t

class MyTimer:
    def __init__(self, func, number=1000000):
        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):
            print(self.func())
            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")
t = MyTimer(3,5)
提示============== RESTART: C:\Users\Administrator\Desktop\shijian.py ==============
>>> t.timing()
Traceback (most recent call last):
  File "<pyshell#9>", line 1, in <module>
    t.timing()
  File "C:\Users\Administrator\Desktop\shijian.py", line 25, in timing
    print(self.func())
TypeError: 'int' object is not callable
>>>
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-3-30 15:23:21 | 显示全部楼层
redaiconglin 发表于 2022-3-30 15:20
import time as t

class MyTimer:

你连 func 都没有先传进去,当然报错,应该是:
  1. t = MyTimer(你自己的一个函数)
复制代码

评分

参与人数 1荣誉 +5 鱼币 +5 收起 理由
isdkz + 5 + 5 鱼C有你更精彩^_^

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-3-30 15:48:05 | 显示全部楼层
redaiconglin 发表于 2022-3-30 15:20
import time as t

class MyTimer:

老师我看小甲鱼老师的这段代码里面没有设置func的函数。如果要传入这个函数应该怎么传。t.func()吗,我不是很理解。谢谢了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-29 10:14

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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