On 04/04/2015 17:49, 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()))
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!


The print function in Python 3 always returns None, which is a false value. As we're talking 'or' here the second part wills always get run, hence in this case sys.exit() gets called.

I'd try the same thing by using your own function.

def myprint(mystr):
    print(mystr)
    return 0

Substitute print in the lambda with myprint and try it, then replace the '0' with a '1' and see what happens.

--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to