[Tips] Python achieved using Reflected static local variables inside a function

def func():
    if not hasattr(func, '_value'):
        func._value = 0 # Initialize "static local variable" _value
    func._value += 1
    print(func._value)
# 测试 for i in range(10): func()

 

Guess you like

Origin www.cnblogs.com/qyxfzmbz/p/12348303.html