On Sat, Apr 4, 2015 at 12:34 PM, Steven D'Aprano <st...@pearwood.info> wrote: > On Sat, Apr 04, 2015 at 11:49:08AM -0500, boB Stepp wrote: >> Windows 7, Python 3.4.3 >> >> This code snippet is "Example 7-13" on page 383 from "Programming >> Python, 4th ed." by Mark Lutz : >> >> import sys >> from tkinter import * >> >> widget = Button(None, >> text='Hello event world!', >> command=(lambda: print('Hello lambda world!') or sys.exit())) > > o_O > > That's either the most horrible misuse of lambda I've ever seen, or a > really cool and rather nifty trick. I'm not sure which :-)
Re-reading the paragraph relevant to Mr. Lutz's example, perhaps I should quote the full paragraph: "This code is a bit tricky because lambdas can contain only an expression; to emulate the original script, this version uses an or operator to force two expressions to be run (print works as the first, because it's a function call in Python 3.X--we don't need to resort to using sys.stdout directly)." I think the key thought of the author here is he showing how to rewrite an earlier example that explicitly defines a quit() function, which both prints and then exits the program, to using a lambda expression. I don't think his intent is to say that the lambda expression is to be preferred (Or is he?). To my mind, would: def quit(): print('Hello lambda world!') sys.exit() and: widget = Button(None, text='Hello event world!', command=quit) be preferable Python style? boB _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor