[Tutor] Use of "or" in a lambda expression
boB Stepp
robertvstepp at gmail.com
Sat Apr 4 18:49:08 CEST 2015
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()))
widget.pack()
widget.mainloop()
My question is about the lambda expression. The author states "...this
version uses an or operator to force two expressions to be run..." I
am not understanding how 'or' causes this to happen. I guess I am
expecting the 'or' to result only in the print running without
executing sys.exit(). But that is not what happens--of course. I tried
substituting 'and' for 'or', but this results in only the print being
run! Obviously I have a significant misunderstanding of what is going
on.
Thanks!
--
boB
More information about the Tutor
mailing list