Modern Python
Python 3.10+ features: type hints, match, dataclasses, pathlib, async.
pythonlanguage
# Modern Python (3.10+)
## Type hints
```py
from typing import Iterable
def total(nums: Iterable[int]) -> int:
return sum(nums)
```
- Use `list[int]`, `dict[str, int]` directly (3.9+).
- `X | None` instead of `Optional[X]` (3.10+).
## match statement
```py
match event:
case {'type': 'click', 'x': x, 'y': y}: ...
case {'type': 'key', 'code': code}: ...
case _: raise ValueError
```
## dataclasses
```py
from dataclasses import dataclass, field
@dataclass(frozen=True, slots=True)
class Point:
x: float
y: float
tags: list[str] = field(default_factory=list)
```
## pathlib (no os.path)
```py
from pathlib import Path
p = Path('data') / 'file.json'
p.write_text(json.dumps(obj))
for f in Path('logs').glob('*.log'): ...
```
## async basics
```py
import asyncio
async def fetch(u): ...
async def main():
results = await asyncio.gather(*(fetch(u) for u in urls))
asyncio.run(main())
```
## Tooling
- `uv` or `poetry` for env/dependency management
- `ruff` for lint+format (replaces flake8/black/isort)
- `pytest` for tests; `mypy` or `pyright` for type-check
API: /api/skills/python-modern