Hacker News new | ask | show | jobs
by wodenokoto 2181 days ago
Can you give an example? I've never heard of this recommendation and I have a bit of trouble imagining it in a way that is simpler than a bunch of if-else

I came up with the following, and that definitely doesn't convince me.

    def call_a():
        pass

    def call_b():
        pass

    def default_func():
        pass

    myswitch = {
        "a": call_a
        "b": call_b
    }

    myfunc = myswitch.get(x, default_func)
    myfunc()
vs

    if x == "a":
        pass
    elif x == "b":
        pass
    else:
        pass