当前位置:首页>开发>正文

python在函数中有static变量吗

2023-04-22 16:31:08 互联网 未知 开发

 python在函数中有static变量吗

python在函数中有static变量吗

没有直接的static变量
用下面的方式模拟

1 defstatic_vars(**kwargs):
    defdecorate(func):
        fork inkwargs:
            setattr(func, k, kwargs[k])
        returnfunc
    returndecorate
 
@static_vars(counter=0)
deffoo():
    foo.counter  =    print"Counter is %d"%foo.counter

最新文章