|
- <pre style="background-color: rgb(43, 43, 43); color: rgb(169, 183, 198); font-family: "JetBrains Mono", 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):
- <span style="color: rgb(204, 120, 50);">def </span><span style="color: rgb(255, 198, 109);">wrapper</span>(func):
- last_time = time.time()
- <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):
- <span style="color: rgb(204, 120, 50);">global </span>last_time
- <span style="color: rgb(204, 120, 50);">global </span>duration
- <span style="color: rgb(204, 120, 50);">while </span>duration >= <span style="color: rgb(104, 151, 187);">0</span>:
- current_time = time.time()
- <span style="color: rgb(204, 120, 50);">if </span>current_time - last_time > step:
- func(*args<span style="color: rgb(204, 120, 50);">, </span>**kwargs)
- last_time = current_time
- duration -= <span style="color: rgb(104, 151, 187);">1
- </span><span style="color: rgb(104, 151, 187);"> </span><span style="color: rgb(204, 120, 50);">else</span>:
- <span style="color: rgb(204, 120, 50);">pass
- </span><span style="color: rgb(204, 120, 50);"> return </span>inner
- <span style="color: rgb(204, 120, 50);">return </span>wrapper
- <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>)
- <span style="color: rgb(204, 120, 50);">def </span><span style="color: rgb(255, 198, 109);">func</span>(x):
- x -= <span style="color: rgb(104, 151, 187);">1
- </span><span style="color: rgb(104, 151, 187);"> </span><span style="color: rgb(136, 136, 198);">print</span>(x)
- <span style="color: rgb(204, 120, 50);">return </span>x
- func(<span style="color: rgb(104, 151, 187);">100</span>)
- </pre><pre style="background-color: rgb(43, 43, 43); color: rgb(169, 183, 198); font-family: "JetBrains Mono", monospace; font-size: 15pt;">
- </pre><pre style="background-color: rgb(43, 43, 43); color: rgb(169, 183, 198); font-family: "JetBrains Mono", 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: "JetBrains Mono", monospace; font-size: 15pt;">
- </pre><pre style="background-color: rgb(43, 43, 43); color: rgb(169, 183, 198); font-family: "JetBrains Mono", monospace; font-size: 15pt;">如图,我想写一个用于程序调用频率的装饰器,可是运行的时候报错,提示我有一个参数未被调用。请各位高手</pre><pre style="background-color: rgb(43, 43, 43); color: rgb(169, 183, 198); font-family: "JetBrains Mono", monospace; font-size: 15pt;">帮忙看看问题到底出在哪里??</pre><pre style="background-color: rgb(43, 43, 43); color: rgb(169, 183, 198); font-family: "JetBrains Mono", monospace; font-size: 15pt;">
- </pre>
复制代码 |
|