# 基本模式匹配 defanalyze_type(data): match data: caseint(): return"整数" casefloat(): return"浮点数" casestr(): return"字符串" caselist(): return"列表" casedict(): return"字典" case _: return"其他类型" #===========#===========#===========#===========#===========#===========#===========#=========== # 结构匹配和变量绑定 defprocess_point(point): match point: case (0, 0): return"原点" case (0, y): returnf"Y轴上的点 y={y}" case (x, 0): returnf"X轴上的点 x={x}" case (x, y) if x == y: returnf"对角线上的点 ({x}, {y})" case (x, y): returnf"普通点 ({x}, {y})"
# 匹配对象属性 classPoint: def__init__(self, x, y): self.x = x self.y = y
defanalyze_point(point): match point: case Point(x=0, y=0): return"原点" case Point(x=0, y=y): returnf"Y轴上的点 y={y}" case Point(x=x, y=0): returnf"X轴上的点 x={x}" case Point(x=x, y=y) if x == y: returnf"对角线上的点 ({x}, {y})" case Point(): returnf"普通点 ({point.x}, {point.y})"