for examplethe following code defines function that makes and returns another functionand represents more common usage patterndef () def ()print(xreturn remembers in enclosing def scope return but don' call it action (action(makereturn function call it nowprints in this codethe call to action is really running the function we named when ran this works because functions are objects in python like everything elseand can be passed back as return values from other functions most importantlyf remembers the enclosing scope' in even though is no longer active--which leads us to the next topic factory functionsclosures depending on whom you askthis sort of behavior is also sometimes called closure or factory function--the former describing functional programming techniqueand the latter denoting design pattern whatever the labelthe function object in question remembers values in enclosing scopes regardless of whether those scopes are still present in memory in effectthey have attached packets of memory ( state retention)which are local to each copy of the nested function createdand often provide simple alternative to classes in this role simple function factory factory functions ( closuresare sometimes used by programs that need to generate event handlers on the fly in response to conditions at runtime for instanceimagine gui that must define actions according to user inputs that cannot be anticipated when the gui is built in such caseswe need function that creates and returns another functionwith information that may vary per function made to illustrate this in simple termsconsider the following functiontyped at the interactive prompt (and shown here without the continuation-line promptsper the presentation note ahead)def maker( )def action( )return * return action make and return action action retains from enclosing scope this defines an outer function that simply generates and returns nested functionwithout calling it--maker makes actionbut simply returns action without running it if we call the outer functionscopes and nested functions