'' ' Nonlocal keyword for the outer layer (non-global) variables in function or other action domains. '' ' DEF Work (): X = 0 DEF new_work (): nonlocal X X = X +. 3 return X return new_work F = Work () Print (F ()) Print (F ()) Print (F ())
Print results
3
6
9
''' 使用global 实现 ''' a =0 def new_work(): global a a=a+3 return a print(new_work()) print(new_work()) print(new_work())
Print results
3
6
9
'' ' Closure = function + environment variables ' '' DEF dosometing (): A = 25 DEF the Add (X): D = A + X return D return the Add A = 10 F = dosometing () Print (F (. 5) ) Print (F (. 5)) Print (F (. 5))
Print results
30
30
30
'' ' Closure function + = environment variable nonlocal keyword for the outer layer (non-global) variables in function or other action domains. '' ' DEF dosometing (): A = 25 DEF the Add (X): nonlocal A A = A + X return A return the Add A = 10 F = dosometing () Print (F (. 5)) Print (F (. 5)) print (f (5))
Print results
30
35
40