Comment

Comment by sfink

You can pry the walrus operator from my cold dead hands.

    m = re.match(pattern1, line)
    if m:
        do_stuff(m.group(1))
    else:
        m = re.match(pattern2, line)
        if m:
            do_other_stuff(m.group(2))
        else:
            m = re.match(pattern3, line)
            if m:
                do_things(m.groups())
            else:
                m = ...
Obviously, there are ways to improve this particular example without using the walrus. But it's also usually not this simple.

I run into something like this all the time. Not many times per program, and I don't actually use the walrus all that often, but when it's the right thing it's so very nice.

parent sfink 1 year ago

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton