for state retentionthoughnonlocal makes it more generally applicable besides allowing names in enclosing defs to be changedthe nonlocal statement also forces the issue for references--much like the global statementnonlocal causes searches for the names listed in the statement to begin in the enclosing defsscopesnot in the local scope of the declaring function that isnonlocal also means "skip my local scope entirely in factthe names listed in nonlocal must have been previously defined in an enclosing def when the nonlocal is reachedor an error is raised the net effect is much like globalglobal means the names reside in the enclosing moduleand nonlocal means they reside in an enclosing def nonlocal is even more strictthough--scope search is restricted to only enclosing defs that isnonlocal names can appear only in enclosing defsnot in the module' global scope or built-in scopes outside the defs the addition of nonlocal does not alter name reference scope rules in generalthey still work as beforeper the "legbrule described earlier the nonlocal statement mostly serves to allow names in enclosing scopes to be changed rather than just referenced howeverboth global and nonlocal statements do tighten up and even restrict the lookup rules somewhatwhen coded in functionglobal makes scope lookup begin in the enclosing module' scope and allows names there to be assigned scope lookup continues on to the built-in scope if the name does not exist in the modulebut assignments to global names always create or change them in the module' scope nonlocal restricts scope lookup to just enclosing defsrequires that the names already exist thereand allows them to be assigned scope lookup does not continue on to the global or built-in scopes in python xreferences to enclosing def scope names are allowedbut not assignment howeveryou can still use classes with explicit attributes to achieve the same changeable state information effect as nonlocals (and you may be better off doing so in some contexts)globals and function attributes can sometimes accomplish similar goals as well more on this in momentfirstlet' turn to some working code to make this more concrete nonlocal in action on to some examplesall run in references to enclosing def scopes work in as they do in --in the followingtester builds and returns the function nestedto be called laterand the state reference in nested maps the local scope of tester using the normal scope lookup rulesc:\codec:\python \python def tester(start)the nonlocal statement in