Lambda returning tuple question, multi-expression

2QdxY4RzWzUUiLuE at potatochowder.com 2QdxY4RzWzUUiLuE at potatochowder.com
Fri Mar 10 22:37:43 EST 2023


On 2023-03-10 at 22:16:05 -0500,
Thomas Passin <list1 at tompassin.net> wrote:

> 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

I might be missing something, but how is that more understandable and
less error prone than any of the following:

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

or:

    cmd = ["tail", "-n", "1", "-f", f"/var/log/{target}"]
    pids[target] or pids.update({target: subprocess.Popen(cmd)})

or:

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

Picking a nit, that's not a good place to continue that line with the
backslash, either.  IMO, "not pids[target]" should be atomic.


More information about the Python-list mailing list