wellalmost there is catch it turns out that within lambda (or def)references to names in the enclosing scope are actually resolved when the generated function is callednot when it is created because of thiswhen the function is later calledsuch name references will reflect the latest or final assignments made to the names anywhere in the enclosing scopewhich are not necessarily the values they held when the function was made this holds true even when the callback function is nested only in module' global scopenot in an enclosing functionin either caseall enclosing scope references are resolved at function call timenot at function creation time this is subtly different from default argument valueswhich are evaluated once when the function is creatednot when it is later called because of thatdefaults can still be useful for remembering the values of enclosing scope variables as they were when you made the function unlike enclosing scope name referencesdefaults will not have different value if the variable later changes in the enclosing scopebetween function creation and call (in factthis is why mutable defaults like lists retain their state between calls--they are created only oncewhen the function is madeand attached to the function itself this is normally nonissuebecause most enclosing scope references name variable that is assigned just once in the enclosing scope (the self argument in class methodsfor examplebut this can lead to coding mistakes if not understoodespecially if you create functions within loopif those functions reference the loop variableit will evaluate to the value it was given on the last loop iteration in all the functions generated by contrastif you use defaults insteadeach function will remember the current value of the loop variablenot the last because of this differencenested scope references are not always sufficient to remember enclosing scope valuesand defaults are sometimes still required today let' see what this means in terms of code consider the following nested function (this section' code snippets are saved in file defaults py in the examples packageif you want to experiment with themdef simple()spam 'nidef action()print(spamreturn action act simple(act(name maps to enclosing function make and return nested function then call itprints 'nithis is the simple case for enclosing scope referencesand it works the same way whether the nested function is generated with def or lambda but notice that this still works if we assign the enclosing scope' spam variable after the nested function is created graphical user interfaces