Lambda returning tuple question, multi-expression

Weatherby,Gerard gweatherby at uchc.edu
Thu Mar 9 05:11:24 EST 2023


Other than the admittedly subjective viewpoint that using the lambda is confusing, there’s probably nothing wrong with the lambda approach. A couple of alternatives:


def e():
     for e in (e1,e2,e3):
         e.config(state="normal")
b = tk.Button(master=main, text="Enable",command=e)

or

b = tk.Button(master=main, text="Enable",command=lambda: [e.config(state="normal") for e in (e1, e2, e3)])

From: Python-list <python-list-bounces+gweatherby=uchc.edu at python.org> on behalf of aapost <aapost at idontexist.club>
Date: Wednesday, March 8, 2023 at 5:15 PM
To: python-list at python.org <python-list at python.org>
Subject: Lambda returning tuple question, multi-expression
*** Attention: This is an external email. Use caution responding, opening attachments or clicking on links. ***

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.

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?


Example 1:

import tkinter as tk

main = tk.Tk()
e1 = tk.Entry(master=main)
e1["state"] = "disabled"
e1.pack()
e2 = tk.Entry(master=main)
e2["state"] = "disabled"
e2.pack()
e3 = tk.Entry(master=main)
e3["state"] = "disabled"
e3.pack()

b = tk.Button(master=main, text="Enable")
b.config(
     command=lambda: (
         e1.config(state="normal"),
         e2.config(state="normal"),
         e3.config(state="normal")
     )
)
b.pack()


Example 2:

import tkinter as tk

main = tk.Tk()
l = tk.Label(master=main)
l.a = {"seconds":0}
l._updater = lambda: (
     l.a.update({"seconds": 1 + l.a["seconds"]}),
     l.config(text=l.a["seconds"]),
     l.after(ms=1000, func=l._updater)
)
l._updater()
l.pack()

--
https://urldefense.com/v3/__https://mail.python.org/mailman/listinfo/python-list__;!!Cn_UX_p3!i8mp9fRKlBLziXCn6-OIC8fNx0LBohis8m6VARp17Igg5036wrTflGiwwptY18Rgkriw5MquUKxe9Fglqpu8FHEy$<https://urldefense.com/v3/__https:/mail.python.org/mailman/listinfo/python-list__;!!Cn_UX_p3!i8mp9fRKlBLziXCn6-OIC8fNx0LBohis8m6VARp17Igg5036wrTflGiwwptY18Rgkriw5MquUKxe9Fglqpu8FHEy$>


More information about the Python-list mailing list