# 知乎某博🐷
gcount = 0
def global_test():
global gcount #当你需要去修改一个全局变量时
gcount+=1
print (gcount)
global_test()
def make_counter():
count = 0
def counter():
nonlocal count #当你在函数中嵌套定义函数时,内层函数引用了外层函数的变量
count += 1
return count # ->int
return counter #-> function: () -> int
def make_counter_test():
mc = make_counter()
print(mc())
print(mc())
print(mc())
make_counter_test()