Python ขั้นสูง — ภาพรวม
Python “ขั้นสูง” คืออะไร?
หัวข้อที่มีชื่อว่า “Python “ขั้นสูง” คืออะไร?”คุณรู้พื้นฐานของ Python แล้ว — ตัวแปร ฟังก์ชัน คลาส collections และ async ระดับขั้นสูงคือจุดที่ปรัชญาการออกแบบของ Python เปล่งประกาย นี่คือเครื่องมือที่ทำให้โค้ดเบส Python ขนาดใหญ่อ่านง่าย เร็ว และดูแลรักษาได้
ถ้าคุณมาจาก TypeScript คุณมีสัญชาตญาณที่แข็งแกร่งเกี่ยวกับ types, generics, decorators, และการทดสอบแบบมีโครงสร้างอยู่แล้ว โมดูลนี้จะแมปสัญชาตญาณเหล่านั้นโดยตรงกับสิ่งเทียบเท่าใน Python และชี้ให้เห็นจุดที่ Python มีความสามารถพิเศษที่ไม่มีคู่ขนานใน TS
สิ่งที่คุณจะได้เรียน
หัวข้อที่มีชื่อว่า “สิ่งที่คุณจะได้เรียน”| บทเรียน | แนวคิดหลัก | คู่ขนานใน TS |
|---|---|---|
| Typing & Protocols | Protocol, TypeVar, TypedDict, Optional/Union | Structural interfaces, generics, mapped types |
| Dataclasses | @dataclass, frozen, field() | Class + interface hybrid, records |
| Metaclasses & Descriptors | __get__/__set__, การสืบทอด type | ไม่มีคู่เทียบ — ฟีเจอร์เฉพาะ Python |
| itertools & functools | chain, groupby, lru_cache, partial | Lodash / Array methods |
| Pattern Matching | match/case กับ structural patterns | switch + destructuring |
| pytest เชิงลึก | Fixtures, parametrize, assertions | Jest: beforeEach, it.each, expect |
| Profiling | timeit, cProfile, การอ่าน stats | Node --prof, console.time |
Mental model สำหรับทั้งโมดูล
หัวข้อที่มีชื่อว่า “Mental model สำหรับทั้งโมดูล”ระบบ type ของ Python เป็นแบบ opt-in และ runtime-invisible — เช่นเดียวกับ type ใน TypeScript ที่หายไปหลัง compilation ความต่างคือ annotations ของ Python ยังคงอยู่เป็น metadata ที่สามารถ inspect ได้ ซึ่งตรงนี้เองที่ dataclasses, Protocol, และ TypedDict ใช้ประโยชน์
// TypeScript — types are a compiler concerninterface Shape { area(): number;}
// Generics are erased at runtimefunction first<T>(arr: T[]): T { return arr[0];}# Python — types are annotations (not enforced)from typing import Protocol, TypeVar
class Shape(Protocol): def area(self) -> float: ...
T = TypeVar('T')
def first(arr: list[T]) -> T: return arr[0]
print(first([10, 20, 30])) # 10วิธีอ่านโมดูลนี้
หัวข้อที่มีชื่อว่า “วิธีอ่านโมดูลนี้”แต่ละบทเรียนเป็นไปตามรูปแบบเดียวกัน:
- TS analogy ก่อน — ยึดกับสิ่งที่คุณรู้อยู่แล้ว
- กลไกของ Python — syntax และ semantics จริง
- Interactive playground (เมื่อแนวคิดนั้น self-contained)
- PyOnly callouts — สิ่งที่ไม่มีคู่เทียบใน TS
- Quiz — ทดสอบความเข้าใจอย่างรวดเร็ว
เรียนตามลำดับ หรือข้ามไปยังบทเรียนที่เกี่ยวข้องกับโปรเจกต์ปัจจุบันของคุณได้เลย