鱼C论坛

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

[已解决]关于被修饰的函数的重新引用问题

[复制链接]
发表于 2020-3-17 22:18:44 | 显示全部楼层 |阅读模式
10鱼币
第一段代码,最后一句对f函数的引用需要加():
  1. import time
  2. class timeslong(object):
  3.     def __init__(self,func):
  4.         self.f = func
  5.     def __call__(self):
  6.         start = time.clock()
  7.         print("It's time starting ! ")
  8.         self.f()
  9.         print("It's time ending ! ")
  10.         end = time.clock()
  11.         return "It's used : %s ." % (end - start)

  12. @timeslong
  13. def f():
  14.     y = 0
  15.     for i in range(10):
  16.         y = y + i + 1
  17.         print(y)
  18.    

  19. print(f())
复制代码


第二段代码,最后一句对f函数的引用没有加():
  1. import time


  2. def call(func):
  3.     start = time.perf_counter()
  4.     print("It's time starting ! ")
  5.     func()
  6.     print("It's time ending ! ")
  7.     end = time.perf_counter()
  8.     return "It's used : %s ." % (end - start)


  9. @call  ##修饰会自动将下面的部分作为一个参数导入到call函数中
  10. def f():
  11.     y = 0
  12.     for i in range(10):
  13.         y = y + i + 1
  14.         print(y)
  15.    

  16. print(f)
复制代码
最佳答案
2020-3-17 22:18:45
本帖最后由 Stubborn 于 2020-3-18 03:40 编辑

虽然不知道你这么做的原因,假如你的问题是为什么有一个加了()有一个为什么没有加的话,你应该去在仔细看下修饰符的使用,或者参考Python cookbook [https://fishc.com.cn/thread-153981-1-1.html]

  1. import time
  2. from functools import wraps


  3. def timethis(function):
  4.     @wraps(function)
  5.     def wrapper(*args, **kwargs):
  6.         start = time.time()
  7.         function(*args, **kwargs)
  8.         print(function.__name__, start - time.time())

  9.     return wrapper

  10. def downout(n):
  11.     while n > 0:
  12.         n -= 1


  13. downout = timethis(downout)
  14. downout(10000)
复制代码

你要看懂末尾两行,作为装饰符的等价调用,套用到你的代码里面会发生什么。当然你也可以尝试,把第二段代码的 print(f) 去掉,看看有什么结果

最佳答案

查看完整内容

虽然不知道你这么做的原因,假如你的问题是为什么有一个加了()有一个为什么没有加的话,你应该去在仔细看下修饰符的使用,或者参考Python cookbook [https://fishc.com.cn/thread-153981-1-1.html] 你要看懂末尾两行,作为装饰符的等价调用,套用到你的代码里面会发生什么。当然你也可以尝试,把第二段代码的 print(f) 去掉,看看有什么结果
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-3-17 22:18:45 | 显示全部楼层    本楼为最佳答案   
本帖最后由 Stubborn 于 2020-3-18 03:40 编辑

虽然不知道你这么做的原因,假如你的问题是为什么有一个加了()有一个为什么没有加的话,你应该去在仔细看下修饰符的使用,或者参考Python cookbook [https://fishc.com.cn/thread-153981-1-1.html]

  1. import time
  2. from functools import wraps


  3. def timethis(function):
  4.     @wraps(function)
  5.     def wrapper(*args, **kwargs):
  6.         start = time.time()
  7.         function(*args, **kwargs)
  8.         print(function.__name__, start - time.time())

  9.     return wrapper

  10. def downout(n):
  11.     while n > 0:
  12.         n -= 1


  13. downout = timethis(downout)
  14. downout(10000)
复制代码

你要看懂末尾两行,作为装饰符的等价调用,套用到你的代码里面会发生什么。当然你也可以尝试,把第二段代码的 print(f) 去掉,看看有什么结果
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-3-18 07:42:02 | 显示全部楼层
???
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-3-19 11:34:58 | 显示全部楼层
Stubborn 发表于 2020-3-18 03:38
虽然不知道你这么做的原因,假如你的问题是为什么有一个加了()有一个为什么没有加的话,你应该去在仔细看 ...

之前不明白,函数修饰其的用法,原来那个修饰之后,会自动添加一个调用的()所以必须添加一个子函数,谢谢提醒
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-9 10:17

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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