gpt4 book ai didi

python - 模拟函数的静态变量

转载 作者:太空宇宙 更新时间:2023-11-04 10:48:48 25 4
gpt4 key购买 nike

<分区>

解决方案:错误是我的函数实际上是一个方法。

我正在尝试使用装饰器来模拟函数的变量。为什么我会在下面的最后一行收到此错误? 未定义全局名称“onecmd”

def static_var(varname, initial_value):
def decorate(func):
setattr(func, varname, initial_value)
return func
return decorate

@static_var("last", None)
def onecmd(self, line):
if line == "lastcmd":
line = onecmd.last
else:
onecmd.last = line

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com