函数中定义static变量
https://blog.csdn.net/weixin_44326002/article/details/95517847
def test():
if ( not hasattr(test,'x') ): #hasattr函数的第一个变量为当前函数名,第二个为变量名,加单引号
test.x = 0 #注意之后使用这个变量时一定要在变量名前加 函数名.
print(test.x) #同上行
test.x += 1 #同上行
test()
test()
test()
test()
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
编辑 (opens new window)
上次更新: 2021/10/11, 12:43:58