of the enclosing function' local names are retained by references within the classor one of its method functions see for more on nested classes as we'll see in later examples ( ' decorators)the outer def in such code serves similar roleit becomes class factoryand provides state retention for the nested class retaining enclosing scope state with defaults in early versions of python (prior to )the sort of code in the prior section failed because nested defs did not do anything about scopes-- reference to variable within in the following would search only the local ( )then global (the code outside )and then built-in scopes because it skipped the scopes of enclosing functionsan error would result to work around thisprogrammers typically used default argument values to pass in and remember the objects in an enclosing scopedef () def ( = )print(xf (remember enclosing scope with defaults (prints this coding style works in all python releasesand you'll still see this pattern in some existing python code in factit' still required for loop variablesas we'll see in momentwhich is why it remains worth studying today in shortthe syntax arg=val in def header means that the argument arg will default to the value val if no real value is passed to arg in call this syntax is used here to explicitly assign enclosing scope state to be retained specificallyin the modified herethe = means that the argument will default to the value of in the enclosing scope--because the second is evaluated before python steps into the nested defit still refers to the in in effectthe default argument remembers what was in the object that' fairly complexand it depends entirely on the timing of default value evaluations in factthe nested scope lookup rule was added to python to make defaults unnecessary for this role--todaypython automatically remembers any values required in the enclosing scope for use in nested defs of coursethe best prescription for much code is simply to avoid nesting defs within defsas it will make your programs much simpler--in the pythonic viewflat is generally better than nested the following is an equivalent of the prior example that avoids nesting altogether notice the forward reference in this code--it' ok to call function defined after the function that calls itas long as the second def runs before the first function is actually called code inside def is never evaluated until the function is actually called scopes