找回密码
 立即注册

扫一扫,访问微社区

QQ登录

只需一步,快速开始

查看: 1268|回复: 1

[求助] 有参装饰器,参数无法被正常调用

1

主题

3

帖子

3

积分

贫民

积分
3
国产跑车 发表于 2022-1-16 10:42:44 | 显示全部楼层 |阅读模式
  1. <pre style="background-color: rgb(43, 43, 43); color: rgb(169, 183, 198); font-family: &quot;JetBrains Mono&quot;, monospace; font-size: 15pt;"><span style="color: rgb(204, 120, 50);">def </span><span style="color: rgb(255, 198, 109);">timer</span>(step<span style="color: rgb(204, 120, 50);">,</span>duration):
  2.     <span style="color: rgb(204, 120, 50);">def </span><span style="color: rgb(255, 198, 109);">wrapper</span>(func):
  3.         last_time = time.time()
  4.         <span style="color: rgb(204, 120, 50);">def </span><span style="color: rgb(255, 198, 109);">inner</span>(*args<span style="color: rgb(204, 120, 50);">, </span>**kwargs):
  5.             <span style="color: rgb(204, 120, 50);">global </span>last_time
  6.             <span style="color: rgb(204, 120, 50);">global </span>duration
  7.             <span style="color: rgb(204, 120, 50);">while </span>duration >= <span style="color: rgb(104, 151, 187);">0</span>:
  8.                 current_time = time.time()
  9.                 <span style="color: rgb(204, 120, 50);">if </span>current_time - last_time > step:
  10.                     func(*args<span style="color: rgb(204, 120, 50);">, </span>**kwargs)
  11.                     last_time = current_time
  12.                     duration -= <span style="color: rgb(104, 151, 187);">1
  13. </span><span style="color: rgb(104, 151, 187);">                </span><span style="color: rgb(204, 120, 50);">else</span>:
  14.                     <span style="color: rgb(204, 120, 50);">pass
  15. </span><span style="color: rgb(204, 120, 50);">        return </span>inner
  16.     <span style="color: rgb(204, 120, 50);">return </span>wrapper

  17. <span style="color: rgb(187, 181, 41);">@timer</span>(<span style="color: rgb(104, 151, 187);">100</span><span style="color: rgb(204, 120, 50);">, </span><span style="color: rgb(104, 151, 187);">10</span>)
  18. <span style="color: rgb(204, 120, 50);">def </span><span style="color: rgb(255, 198, 109);">func</span>(x):
  19.     x -= <span style="color: rgb(104, 151, 187);">1
  20. </span><span style="color: rgb(104, 151, 187);">    </span><span style="color: rgb(136, 136, 198);">print</span>(x)
  21.     <span style="color: rgb(204, 120, 50);">return </span>x

  22. func(<span style="color: rgb(104, 151, 187);">100</span>)
  23. </pre><pre style="background-color: rgb(43, 43, 43); color: rgb(169, 183, 198); font-family: &quot;JetBrains Mono&quot;, monospace; font-size: 15pt;">
  24. </pre><pre style="background-color: rgb(43, 43, 43); color: rgb(169, 183, 198); font-family: &quot;JetBrains Mono&quot;, monospace; font-size: 15pt;">>>>NameError: name 'duration' is not defined</pre><pre style="background-color: rgb(43, 43, 43); color: rgb(169, 183, 198); font-family: &quot;JetBrains Mono&quot;, monospace; font-size: 15pt;">
  25. </pre><pre style="background-color: rgb(43, 43, 43); color: rgb(169, 183, 198); font-family: &quot;JetBrains Mono&quot;, monospace; font-size: 15pt;">如图,我想写一个用于程序调用频率的装饰器,可是运行的时候报错,提示我有一个参数未被调用。请各位高手</pre><pre style="background-color: rgb(43, 43, 43); color: rgb(169, 183, 198); font-family: &quot;JetBrains Mono&quot;, monospace; font-size: 15pt;">帮忙看看问题到底出在哪里??</pre><pre style="background-color: rgb(43, 43, 43); color: rgb(169, 183, 198); font-family: &quot;JetBrains Mono&quot;, monospace; font-size: 15pt;">
  26. </pre>
复制代码
回复

使用道具 举报

1

主题

3

帖子

3

积分

贫民

积分
3
国产跑车  楼主| 发表于 2022-1-16 10:46:23 | 显示全部楼层
  1. def timer(step,duration):
  2.     def wrapper(func):
  3.         last_time = time.time()
  4.         def inner(*args, **kwargs):
  5.             global last_time
  6.             global duration
  7.             while duration >= 0:
  8.                 current_time = time.time()
  9.                 if current_time - last_time > step:
  10.                     func(*args, **kwargs)
  11.                     last_time = current_time
  12.                     duration -= 1
  13.                 else:
  14.                     pass
  15.         return inner
  16.     return wrapper

  17. @timer(100, 10)
  18. def func(x):
  19.     x -= 1
  20.     print(x)
  21.     return x

  22. func(100)
复制代码

上面那个 排版好像有问题,而且我找不到编辑的选项。
具体问题就是我在编写一个用于控制函数调用时间的装饰器时,提示我有一个参数无法调用。请问这是什么原因造成的???
回复 支持 反对

使用道具 举报

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

本版积分规则

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