While循环和for循环同样拥有else分支
用break不会执行循环所拥有的else分支
global关键字用来在函数或者其他局部作用域中使用全局变量
gcount = 0
def global_test():
print (gcount)
def global_counter():
global gcount
gcount +=1
return gcount
def global_counter_test():
print(global_counter())
print(global_counter())
print(global_counter())
nonlocal关键字用来在函数或其他作用域中使用外层(非全局)变量
def make_counter():
count = 0
def counter():
nonlocal count
count += 1
return count
return counter
def make_counter_test():
mc = make_counter()
print(mc())
print(mc())
print(mc())