示例工程 02-syntax-c-go-java

02-syntax-c-go-java/04-match-patterns/README.md

markdown · 0.8 KB

# 04 · match-patterns
 
**演示**:`match` 的结构模式(序列/映射/类/守卫),它不是 C 的 `switch`,而是解构 + 条件匹配。
**运行**:
 
```sh
uv run python examples/02-syntax-c-go-java/04-match-patterns/match_patterns.py
```
 
**预期输出**(3.12.9,repr 会随构造器变化):
 
```text
                                  [1, 2] -> pair:1,2
         {'kind': 'user', 'name': 'Ada'} -> user:Ada
                         Point(x=0, y=0) -> origin
                         Point(x=3, y=3) -> diagonal:3
                         Point(x=1, y=2) -> point:1,2
                               '#python' -> tag:python
                               (1, 2, 3) -> other
```
 
**依赖**:仅标准库。
**注意**:`case Point(x=0, y=0)` 依赖 dataclass 生成的 `__match_args__`/属性;普通类需自己声明。