Lambda returning tuple question, multi-expression

Thomas Passin list1 at tompassin.net
Fri Mar 10 22:16:05 EST 2023


On 3/10/2023 7:07 PM, aapost wrote:
> which does start to break down readability due to line length, as there 
> isn't really an indention rule set for something uncommonly used.
> 
> but some renaming makes the pattern clearer
> 
> pids.update({"messages" :subprocess.Popen(["cmd1"])}) if not 
> pids["messages"] else None,
> pids.update({"syslog" :subprocess.Popen(["cmd2"])}) if not 
> pids["syslog"] else None,
> pids.update({"kern" :subprocess.Popen(["cmd3"])}) if not pids["kern"] 
> else None,
> pids.update({"user" :subprocess.Popen(["cmd4"])}) if not pids["user"] 
> else None,

I'd make the pattern in this example even more understandable and less 
error-prone:

def update_pids(target):
     cmd = ["tail", "-n", "1", "-f", f"/var/log/{target}"]
     pids.update({target: subprocess.Popen(cmd)}) if not \
         pids[target] else None

lambda x: ( # The Tk callback includes an event arg, doesn't it?
             update_pids('messages'),
             update_pids('syslog'),
             # etc
           )



More information about the Python-list mailing list