Skip to content

Commit 27f4940

Browse files
authored
Create do_match.py
1 parent b354863 commit 27f4940

1 file changed

Lines changed: 54 additions & 0 deletions

File tree

samples/basic/do_match.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
# 简单匹配:
5+
print('-- simple match --')
6+
7+
score = 'A'
8+
9+
match score:
10+
case 'A':
11+
print('score is A.')
12+
case 'B':
13+
print('score is B.')
14+
case 'C':
15+
print('score is C.')
16+
case _:
17+
print('score is ???.')
18+
19+
# 复杂匹配:
20+
print('-- complex match --')
21+
22+
age = 15
23+
24+
match age:
25+
case x if x < 10:
26+
print('< 10 years old.')
27+
case 10:
28+
print('10 years old.')
29+
case 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18:
30+
print('11~18 years old.')
31+
case 19:
32+
print('19 years old.')
33+
case _:
34+
print('not sure.')
35+
36+
# 列表匹配:
37+
print('-- match list --')
38+
39+
args = ['gcc', 'hello.c', 'world.c']
40+
# args = ['clean']
41+
# args = ['gcc']
42+
43+
match args:
44+
# 如果仅出现gcc,报错:
45+
case ['gcc']:
46+
print('gcc: missing source file(s).')
47+
# 出现gcc,且至少指定了一个文件:
48+
case ['gcc', file1, *files]:
49+
print('gcc compile: ' + file1 + ', ' + ', '.join(files))
50+
# 仅出现clean:
51+
case ['clean']:
52+
print('clean')
53+
case _:
54+
print('invalid command.')

0 commit comments

Comments
 (0)