--- name: python-skill description: Python coding best practices and patterns. Use when writing, reviewing, or debugging Python code. --- # Python Skill ## Type Hints (Python 3.10+) ```python # Bad def process(data, callback): return callback(data) # Good from typing import Callable def process(data: dict, callback: Callable[[dict], str]) -> str: return callback(data) ``` ## Dataclasses Over Plain Dicts ```python from dataclasses import dataclass @dataclass class GameFrame: timestamp: float objects: list[str] confidence: float = 1.0 ``` ## Context Managers for Resources ```python # Bad f = open("log.txt") data = f.read() f.close() # Good with open("log.txt") as f: data = f.read() ``` ## List Comprehensions vs Loops ```python # Prefer comprehension for simple transforms enemies = [obj for obj in objects if obj.type == "enemy"] # Use loop when logic is complex (>2 conditions) results = [] for obj in objects: if obj.type == "enemy" and obj.visible and obj.distance < 100: results.append(obj.position) ``` ## Error Handling ```python # Be specific — never catch bare Exception silently try: frame = capture_screen() except ScreenCaptureError as e: logger.error("Screen capture failed: %s", e) raise ```