Use when writing or editing Python 3.12+ for APIs, data processing, scripts, or tooling. Triggers on `.py` files and prompts about async code, type hints, dataclasses, pathlib, pytest, generators, or context managers, even when the user doesn't say 'Python'.
Installs into .claude/skills of the current project.
Sponsors
SSkills DirectorySkills Directory
Know which skills are safe — weekly.
SSkills DirectorySkills Directory
Is that skill safe to install?
Are you the author of Writing Python?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/xonovex-writing-python)
---
name: writing-python
description: "Use when writing or editing Python 3.12+ for APIs, data processing, scripts, or tooling. Triggers on `.py` files and prompts about async code, type hints, dataclasses, pathlib, pytest, generators, or context managers, even when the user doesn't say 'Python'."
---
# Python Coding Guidelines
Write typed Python with focused functions, explicit errors, and tests that check observable behavior.
## Requirements
- Python ≥ 3.12; pytest ≥ 8.
## Essentials
- **Data models** - Use dataclasses and type hints, see [references/dataclasses-type-hints.md](references/dataclasses-type-hints.md), [references/type-checking.md](references/type-checking.md)
- **Iteration** - Prefer generators/comprehensions for data processing, see [references/generators-comprehensions.md](references/generators-comprehensions.md)
- **Performance** - Cache pure functions with `@cache`, see [references/caching-functions.md](references/caching-functions.md)
- **Resource management** - Use context managers for cleanup, see [references/resource-management.md](references/resource-management.md)
- **Modern syntax** - Use pathlib, f-strings, specific exceptions, see [references/pathlib-file-ops.md](references/pathlib-file-ops.md), [references/string-formatting.md](references/string-formatting.md), [references/exception-handling.md](references/exception-handling.md)
- **Paradigm** - Functional style → **applying-fp**; class/OO design → **applying-oop**
## Gotchas
- Mutable default arguments (`def f(x=[]):`) share state across calls: use `None` and assign inside
- The GIL serializes pure-Python execution: threads only help on I/O; CPU-bound work needs `multiprocessing` or compiled extensions
- `is` checks identity, not equality: small-int caching means `a is b` works for `1` but fails for `300`
- `__init__.py` is no longer required for packages (PEP 420), but mixing namespace and regular packages causes silent import-shadowing bugs
## Progressive disclosure
- Read [references/dataclasses-type-hints.md](references/dataclasses-type-hints.md) - Load when defining structured data models or adding type annotations
- Read [references/type-checking.md](references/type-checking.md) - Load when using Protocols, type aliases, or complex Union types
- Read [references/resource-management.md](references/resource-management.md) - Load when working with files, connections, or resources needing cleanup
- Read [references/caching-functions.md](references/caching-functions.md) - Load when optimizing expensive computations or repeated function calls
- Read [references/generators-comprehensions.md](references/generators-comprehensions.md) - Load when processing large datasets or streaming data
- Read [references/string-formatting.md](references/string-formatting.md) - Load when formatting output, building messages, or templating
- Read [references/pathlib-file-ops.md](references/pathlib-file-ops.md) - Load when reading/writing files or traversing directories
- Read [references/exception-handling.md](references/exception-handling.md) - Load when defining error handling or creating custom exceptions