test = 'hello'
def make_counter():
count = 0
#声明变量为全局变量,需要使用global
global test
test = 'world'
def counter():
#声明变量为函数体内变量,需要使用count
nonlocal count
count += 1
return count,test
return counter
def make_counter_test():
mc = make_counter()
print(mc())
print(mc())
print(mc())
make_counter_test()
转载于:https://www.cnblogs.com/pslblog/p/10239045.html