最简单的就是最常用的,也是最值得优化的。
而且,谁告诉你跟泛型没关系了?随便写个例子,python3.10适用。
import math
for v in ['yes', 1, math.pi, [1,2], {3,4}, (5,6)]:
match v:
case x if isinstance(x,str):
t = 'str'
case x if isinstance(x,int):
t = 'int'
case x if isinstance(x,float):
t = 'float'
case x:
t = 'other'
print(f'{t}: {x}')
str: yes
int: 1
float: 3.141592653589793
other: [1, 2]
other: {3, 4}
other: (5, 6)
【 在 philbloo 的大作中提到: 】
: 你说的表驱动是给 c 那种 switch case 用的,那种最简单,就一 jmp table ,可以直接 lower 到 ISA 上。但 pattern match 是 np hard,跟 switch case 不是一个问题,跟泛型也没有关系。
--
修改:poocp FROM 222.212.168.*
FROM 222.212.168.*