match / case: match a value against several patterns Match / Case: เทียบค่าหลายแบบ
Use match with case (Python 3.10+) instead of a long if/elif when matching one value many ways
ใช้ match กับ case (Python 3.10+) แทน if/elif ยาวๆ เมื่อเทียบค่าหลายทาง
match / case is structural pattern matching — a value is compared against each
case from top to bottom, and the first matching block runs (later cases are not checked). It is not
the same as switch in C, but it is used similarly to compare one value many ways.
match / case คือ structural pattern matching — เอาค่าไปเทียบกับแต่ละ case จากบนลงล่าง
เจอแบบที่ตรงแล้วรันบล็อกนั้น (ไม่เช็ค case ถัดไป)
ต่างจาก switch ในภาษา C แต่ใช้แทนการเทียบค่าหลายทางได้คล้ายกัน
1 match a number with several cases 1 match ค่าตัวเลข + หลาย case
Write match value: followed by a case pattern: for each pattern — indent the
block under each case.
เขียน match ค่า: แล้วตามด้วย case แบบ: แต่ละแบบ — ย่อหน้าบล็อกใต้ case
Wed
2 Several values in one case with | 2 case หลายค่าด้วย |
Use | to combine several values in one case — matching any one of them enters that block.
ใช้เครื่องหมาย | รวมหลายค่าใน case เดียว — ตรงค่าใดค่าหนึ่งก็เข้าบล็อกนั้น
two or three
3 case _ as the default 3 case _ เป็นค่าเริ่มต้น
case _: is the wildcard — it catches every value that did not match an earlier case (like
else for an if).
case _: คือ wildcard — จับทุกค่าที่ยังไม่ตรง case ก่อนหน้า (คล้าย else ของ if)
go or unknown
4 When to use match instead of if/elif 4 เมื่อไหร่ใช้ match แทน if/elif
When you compare the same value against several exact options, match reads more
clearly than a long if/elif. (For number ranges such as >= 80, if/elif still fits better.)
เมื่อเทียบค่าเดียวกันกับหลายทางตรงๆ match อ่านง่ายกว่า if/elif ยาว
(ถ้าเป็นช่วงตัวเลขเช่น >= 80 ยังเหมาะกับ if/elif มากกว่า)
saving
5 A command menu 5 เมนูคำสั่งของเครื่อง
Programs often react to a command typed by the user. match compares the text with each case; | lets one case accept several spellings, and case _ catches everything else.
โปรแกรมมักตอบสนองต่อคำสั่งที่ผู้ใช้พิมพ์ match เทียบข้อความกับแต่ละ case ใช้ | ให้หนึ่ง case รับได้หลายคำ และ case _ ดักทุกกรณีที่เหลือ
Machine stopping
6 Common mistakes 6 ข้อผิดพลาดที่พบบ่อย
-
Expecting fall-through. Unlike
switchin C, only the first matchingcaseruns and there is nobreak. The demo printstwoonly. -
คิดว่าจะรันต่อทุก case ต่างจาก
switchในภาษา C เพราะcaseแรกที่ตรงเท่านั้นที่ทำงาน และไม่ต้องใช้breakตัวอย่างพิมพ์เพียงtwo -
case _in the wrong place. The wildcard matches anything, so it must be the last case; Python reports aSyntaxErrorif other cases follow it. -
วาง
case _ผิดที่ wildcard ตรงกับทุกค่า จึงต้องเป็น case สุดท้าย ถ้ามี case อื่นตามหลัง Python จะแจ้งSyntaxError -
A bare name is not a constant.
case x:does not compare with a variablex; it matches everything and stores the value inx. Use literal values incase. -
ชื่อตัวแปรเปล่า ๆ ไม่ใช่ค่าคงที่
case x:ไม่ได้เทียบกับตัวแปรxแต่ตรงกับทุกค่าแล้วเก็บค่าลงในxให้ใช้ค่าตรง ๆ ในcase
two
7 Think further 7 แบบฝึกคิดต่อ
Try these yourself in the Run boxes above (or in Google Colab):
ลองทำเองในกล่อง Run ด้านบน (หรือใน Google Colab):
-
Turn a weekday number (1–7) into its name with
match. Usecase 6 | 7to printweekend. -
แปลงเลขวัน (1–7) เป็นชื่อวันด้วย
matchและใช้case 6 | 7ให้พิมพ์weekend -
Can you rewrite the
if / elifgrade chain from the previous lesson withmatch? Why is it awkward for ranges such as>= 80? -
ลองเขียนเกรดด้วย
if / elifจากบทก่อนใหม่ด้วยmatchทำไมจึงไม่สะดวกกับช่วงตัวเลขอย่าง>= 80 -
Add a fourth command
"pause"to the menu and test an unknown word. -
เพิ่มคำสั่งที่สี่
"pause"ลงในเมนู แล้วทดสอบคำที่ไม่รู้จัก
Key takeaways สรุปสำคัญ
Remember these points when you write your own programs:
จำประเด็นเหล่านี้ไว้เมื่อเขียนโปรแกรมของคุณเอง:
-
matchis best when one value is compared with several fixed alternatives, such as commands, codes or menu choices. -
matchเหมาะเมื่อต้องเทียบค่าเดียวกับตัวเลือกคงที่หลายตัว เช่น คำสั่ง รหัส หรือเมนู -
Only the first matching
caseruns, so there is nobreakand no fall-through to the next case. -
caseแรกที่ตรงเท่านั้นที่ทำงาน จึงไม่ต้องใช้breakและไม่ไหลต่อไปยัง case ถัดไป -
Use
|to group values that should behave the same, and end withcase _so unexpected input is handled instead of ignored silently. -
ใช้
|รวมค่าที่ต้องทำงานเหมือนกัน และปิดท้ายด้วยcase _เพื่อจัดการค่าที่คาดไม่ถึงแทนที่จะเงียบไปเฉย ๆ -
For ranges such as
score >= 80, anif / elifchain is still the clearer tool; choose the structure that makes the intention obvious to the next reader. -
สำหรับช่วงตัวเลขอย่าง
score >= 80โซ่if / elifยังอ่านง่ายกว่า เลือกโครงสร้างที่ทำให้ผู้อ่านคนถัดไปเข้าใจเจตนาได้ชัดที่สุด -
A
matchblock compares by pattern, not by variable name, so keep thecaselines simple and literal. -
บล็อก
matchเทียบตาม pattern ไม่ใช่ตามชื่อตัวแปร จึงควรเขียนบรรทัดcaseให้เรียบง่ายและเป็นค่าตรง ๆ
Note: Run on this page is a JavaScript simulation, not real Python. For real Python, try Google Colab. หมายเหตุ: การกด Run บนหน้านี้เป็นการจำลองด้วย JavaScript ไม่ใช่ Python จริง หากต้องการทดลอง Python จริง แนะนำ Google Colab
References References / แหล่งอ้างอิง
Sources used for this lesson:
แหล่งที่ใช้ประกอบเนื้อหาในบทนี้:
-
The match statement —
Python Language Reference — match
(
match/case/_)