스킬 python-best-practices
🐍
撰寫 Python 而不使用類型定義會導致運行時錯誤和難以維護的程式碼。本技能提供經過實戰測試的類型優先開發模式,使用 dataclasses、可識別聯合、Protocols 和其他現代 Python 功能,讓非法狀態無法被表示。
지원: Claude Codex Code(CC)
1
스킬 ZIP 다운로드
2
Claude에서 업로드
설정 → 기능 → 스킬 → 스킬 업로드로 이동
3
토글을 켜고 사용 시작
테스트해 보기
"python-best-practices" 사용 중입니다. Create a frozen dataclass for a product with required name and price, optional description
예상 결과:
- Use @dataclass(frozen=True) for immutability
- Mark required fields without defaults first
- Add optional fields with default values after required fields
- The frozen=True flag prevents accidental mutation
- Example output shows proper field ordering and type hints
보안 감사
안전v2 • 1/10/2026
Pure documentation skill containing only markdown guidance. No executable code, no file system access, no network calls, and no external command execution. This skill only provides informational content when loaded by an AI agent.
1
스캔된 파일
271
분석된 줄 수
0
발견 사항
2
총 감사 수
보안 문제를 찾지 못했습니다
감사자: claude 감사 이력 보기 →
품질 점수
38
아키텍처
100
유지보수성
83
콘텐츠
30
커뮤니티
100
보안
78
사양 준수
만들 수 있는 것
設計類型安全的資料模型
學習使用 dataclass、NewType 和可識別聯合在類型層級編碼領域約束。
審查 Python 類型模式
使用 Protocols、TypedDict 和窮舉匹配聯合在程式碼庫中應用一致的類型模式。
生成慣用的 Python 程式碼
在使用 Claude、Codex 或 Claude Code 時產生遵循現代類型優先模式的 Python 程式碼。
이 프롬프트를 사용해 보세요
建立資料模型
Create a frozen dataclass with proper type hints for a user profile including required fields and optional avatar. Use the python-best-practices patterns.
處理狀態機
Model a request state machine with idle, loading, success, and failure states using discriminated unions and pattern matching. Apply python-best-practices patterns.
新增類型安全
Create NewType wrappers for UserId and OrderId to prevent mixing them up. Show how to validate input and create the wrapped types.
定義介面
Define a Protocol for a file-like object with read method, then show how to use it as a type hint for functions that accept any read-compatible object.
모범 사례
- 在實現之前先定義類型;讓類型檢查器引導完整性
- 使用 frozen dataclasses 和不可變模式來防止意外的狀態變化
- 在系統邊界使用運行時檢查配合類型提示來驗證資料
피하기
- 在函數簽名中使用可變的預設參數
- 為「明顯」的返回類型跳過類型提示
- 捕獲異常而不重新引發或添加上下文
자주 묻는 질문
這些模式支持哪些 Python 版本?
模式匹配需要 Python 3.10+。Dataclasses 和 typing 功能可在 3.7+ 上運行。對於較舊的專案使用 pyright 或 mypy。
這與 Pydantic 相比如何?
Dataclasses 提供編譯時類型。Pydantic 新增運行時驗證。兩者一起使用可獲得最大的安全性。
我可以將其與 FastAPI 或 Django 一起使用嗎?
可以。這些模式補充了 Web 框架。使用 dataclasses 定義模型,在路由處理器中使用它們。
這個技能會存取我的程式碼嗎?
不。這是純知識技能。它提供指導但無法讀取、寫入或執行您的程式碼。
我應該使用什麼類型檢查器?
pyright 提供最佳的推斷和 VS Code 整合。mypy 有更多插件。ty 是大型程式碼庫中最快的。
什麼時候應該使用 TypedDict 而不是 dataclass?
對內部 Python 物件使用 dataclasses。當匹配外部 JSON 或 dict 結構時使用 TypedDict。