created within the loops if you're going to make functions within loopyou have to apply the last example' behavior to the loop variabledef odd()funcs [for in 'abcdefg'funcs append((lambdac)return funcs will be looked up later does not remember current for func in odd()print(func()end='oopsprint 'snot , ,cherethe func list simulates registered gui callback handlers associated with widgets this doesn' work the way most people expect it to the variable within the nested function will always be herethe value that the variable was set to on the final iteration of the loop in the enclosing scope the net effect is that all seven generated lambda functions wind up with the same extra state information when they are later called analogous gui code that adds information to lambda callback handlers will have similar problems--all buttons created in loopfor instancemay wind up doing the same thing when clickedto make this workwe still have to pass values into the nested function with defaults in order to save the current value of the loop variable (not its future value)def odd()funcs [for in 'abcdefg'funcs append((lambda =cc)return funcs force to remember now defaults eval now for func in odd()print(func()end='oknow prints , ,cthis works now only because the defaultunlike an external scope referenceis evaluated at function creation timenot at function call time it remembers the value that name in the enclosing scope had when the function was madenot the last assignment made to that name anywhere in the enclosing scope the same is true even if the function' enclosing scope is modulenot another functionif we don' use the default argument in the following codethe loop variable will resolve to the same value in all seven functionsfuncs [for in 'abcdefg'funcs append((lambda =cc)enclosing scope is module force to remember now else prints ' again for func in funcsprint(func()end='okprints , ,cthe moral of this story is that enclosing scope name references are replacement for passing values in with defaultsbut only as long as the name in the enclosing scope will graphical user interfaces