act action( act( ((lambda (lambda yx ))( ))( herethe nested lambda structure makes function that makes function when called in both casesthe nested lambda' code has access to the variable in the enclosing lambda this worksbut it seems fairly convoluted codein the interest of readabilitynested lambdas are generally best avoided why you will carelambda callbacks another very common application of lambda is to define inline callback functions for python' tkinter gui api (this module is named tkinter in python xfor examplethe following creates button that prints message on the console when pressedassuming tkinter is available on your computer (it is by default on windowsmaclinuxand other oss)import sys from tkinter import buttonmainloop tkinter in buttontext='press me'command=(lambda:sys stdout write('spam\ ')) pack(mainloop(this may be optional in console mode xprint(herewe register the callback handler by passing function generated with lambda to the command keyword argument the advantage of lambda over def here is that the code that handles button press is right hereembedded in the button-creation call in effectthe lambda defers execution of the handler until the event occursthe write call happens on button pressesnot when the button is createdand effectively "knowsthe string it should write when the event occurs because the nested function scope rules apply to lambdas as wellthey are also easier to use as callback handlersas of python --they automatically see names in the functions in which they are coded and no longer require passed-in defaults in most cases this is especially handy for accessing the special self instance argument that is local variable in enclosing class method functions (more on classes in part vi)class myguidef makewidgets(self)button(command=(lambdaself onpress("spam"))def onpress(selfmessage)use message in early versions of pythoneven self had to be passed in to lambda with defaults as we'll see laterclass objects with __call__ and bound methods often serve in callback roles too--watch for coverage of these in and anonymous functionslambda