Featured image of post Python小技巧 - first-class 功能

Python小技巧 - first-class 功能

由于Python具有 fisrt-class 的功能,因此它们可用于模拟switch / case语句

先看下一个if逻辑判断函数

代码如下


def if_func(operator, x, y):
    if operator == 'add':
        return x + y
    elif operator == 'sub':
        return x - y
    elif operator == 'mul':
        return x * y
    elif operator == 'div':
        return x / y
    else:
        return None

上面代码我们能很清晰的看清楚这个函数的具体功能

然后我们参考上面的代码逻辑新写一个


def dict_func(operator, x, y):
    return {
        'add': lambda: x + y,
        'sub': lambda: x - y,
        'mul': lambda: x * y,
        'div': lambda: x / y
    }.get(operator, lambda: None)()

下面我们测试下两个函数的结果


print(if_func('add', 1, 2))
print(dict_func('add', 1, 2))
print(if_func('mul', 3, 4))
print(dict_func('mul', 3, 4))

执行结果如下


3
3
12
12