python:一种代替 switch--

2023-01-31 05:01:16 python switch

除了使用 if-elif-else 之外,还可以这样用

def add(a, b):
    return a + b

def minus(a, b):
    return a - b

def main(mod, a, b):
    return {
        "add": add(a, b),
        "minus": minus(a, b)
        }.get(mod, None)  # default return is "None", when there is no answer is dict.

if __name__ == "__main__":
    print main("add", 1, 2)
    print main("minus", 9, 2)
    print main("fuck", 9, 2)

相关文章