Hi, Thus spoketh "守株待兔" <1248283...@qq.com> unto us on Thu, 18 Aug 2011 12:09:52 +0800:
> there is a code: > #coding:utf-8 > from Tkinter import * > from sys import stdout, exit # lambda generates a > function > widget = Button(None, # but contains just an > expression text='Hello event world', > command=(lambda: stdout.write('Hello lambda world\n') or > exit()) ) > widget.pack() > widget.mainloop() > > when you click the button,there is output : Hello lambda world, then > exit the program. i am confused, why not use "and" ? i made a try, if > i use "or",the program will not exit ,why? You seem to expect that the "and" inside the body of your lamdba function will force the interpreter to execute first the stdout.write() command "and" then the exit() command. Actually however using "and" or "or" in your lambda will cause the whole expression to be evaluated as True or False. Now what happens is: First case: stdout.write('Hello lambda world\n') and exit() This evaluates to True if *both* parts are True. So the interpreter first evaluates stdout.write('Hello lambda world\n') . The return value of this call is "None" which evaluates to False so the whole expression will evaluate to False, no matter what the second part does - the interpreter will not even try. Second case: stdout.write('Hello lambda world\n') or exit() This evaluates to True if *one or both* parts are True. Now again the interpreter tries stdout.write() first, gets a "False" and so tries to check the result of the second statement. Unfortunately it never gets to see the result, because exit() stops the interpreter ;) Maybe this behavior is easier to understand if you try something like this at a python prompt: >>> from sys import stdout >>> print stdout.write('foo\n') foo None >>> print stdout.write('foo\n') or stdout.write('bar\n') foo bar None >>> print stdout.write('foo\n') and stdout.write('bar\n') foo None >>> if stdout.write('foo\n'): ... print 'True' ... else: ... print 'False' ... foo False >>> I hope this helps Michael .-.. .. ...- . .-.. --- -. --. .- -. -.. .--. .-. --- ... .--. . .-. There's no honorable way to kill, no gentle way to destroy. There is nothing good in war. Except its ending. -- Abraham Lincoln, "The Savage Curtain", stardate 5906.5 _______________________________________________ Tkinter-discuss mailing list Tkinter-discuss@python.org http://mail.python.org/mailman/listinfo/tkinter-discuss