On 08/03/2023 21:56, aapost wrote: > When making a UI there are a lot of binding/trace operations that need > to occur that lead to a lot of annoying 1 use function definitions. I > don't really see lambda use like below.
Lambdas are very common in GUI callbacks but I admit I've never seen tuples used to create multiple expressions. That's a neat trick I hadn't thought of and will probably use. > Giving 2 working lambda examples using a returned tuple to accomplish > multiple expressions - what sort of gotchas, if any, might make the > following bad practice if I am missing something? Not bad practice per-se but you are still limited to expressions, no loops for example (although you could fake it with a list comp, but that gets ugly fast!) Also layout is all important here. It could get very messy to read if indentation isn't clear. You only have to look at some Javascript code with function definitions as arguments to functions to see how clunky that can be. Similarly debugging so much code passed as arguments might be an issue - no easy way to step into the lambda. But as an alternative to writing many typical event handlers it's definitely a valid approach that I'll be trying. > b = tk.Button(master=main, text="Enable") > b.config( > command=lambda: ( > e1.config(state="normal"), > e2.config(state="normal"), > e3.config(state="normal") > ) > ) You could of course single line that with: b = tk.Button(master=main, text="Enable", command=lambda: ( e1.config(state="normal"), e2.config(state="normal"), e3.config(state="normal") ) ) It's not a radical change from using a lamba as a callback but it does extend the use case to cover a common GUI scenario. I like it. I wish I'd thought of it years ago. Thanks for sharing. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos -- https://mail.python.org/mailman/listinfo/python-list