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

Python小技巧 - first-class 功能

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

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

代码如下

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12

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

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

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

1
2
3
4
5
6
7
8

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)()

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

1
2
3
4
5

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

执行结果如下

1
2
3
4
5

3
3
12
12
Licensed under CC BY-NC-SA 4.0
最后更新于 Jul 30, 2025 10:41 +0800