Readable Python
Write Python so a reader can understand the main operation from top to bottom without tracing layers of indirection. Preserve the repository's established behavior and public interfaces unless the user asks to change them.
Design the module
- Identify the module's inputs, processing steps, outputs, state, and public API.
- Express the primary operation as a short, visible workflow.
- Extract a helper only when the extracted operation has one clear purpose and a descriptive name makes the caller easier to read.
- Keep a new helper immediately below its caller. Move it only when multiple operations genuinely share it.
- Prefer a small amount of obvious duplication over an abstraction that hides behavior.
- Split a module only when the resulting module owns a cohesive responsibility.
Write functions as simple machines
- Treat each function as: typed input, visible processing, typed output.
- Give each function one meaningful operational responsibility. Do not reduce functions to arbitrary line-count targets.
- Keep decisions and side effects apparent in the main workflow.
- Pass required values explicitly instead of relying on hidden mutable state.
- Prefer positional arguments for required inputs when their meaning and order are clear from the function name and signature.
- Use keyword arguments when they clarify optional settings, booleans, repeated primitive values, or otherwise ambiguous calls.
- Avoid
*args and **kwargs unless forwarding an existing compatible interface requires them.
- Return a value with a clear meaning. Avoid modes controlled by several boolean flags.
- Use early returns when they make exit conditions easier to see.
Write only useful docstrings
- Omit a function or method docstring when its name, typed signature, and implementation already make its behavior clear.
- When a docstring adds value, keep it short and explain what the operation does.
- Include only material caveats, preconditions, side effects, or behavior that a reader cannot infer from the signature.
- Do not repeat parameter names, return types, or implementation steps that the code already communicates.
Use classes only for state or lifecycle
- Introduce a class when an operation owns persistent state, configuration, or a resource lifecycle.
- Keep
__init__ limited to validating and storing dependencies or configuration. Do not perform network, file, or browser operations in it.
- Expose a small set of public methods named after real operations.
- Keep private helper methods rare. If a helper does not need instance state, make it a nearby module-level function.
- Do not add factories, managers, service layers, base classes, or strategy objects before the code has a concrete need for them.
Choose operational names
- Prefer names that describe what the code does in the domain:
capture_browser_observation, choose_discovery_action, or store_successful_correction.
- Name functions with a verb and the object or result of the operation.
- Name booleans as questions or conditions, such as
is_registration_page or has_visible_captcha.
- Use plural names for collections and include units in values such as
timeout_seconds.
- Avoid vague names such as
process, handle, manage, execute, data, item, object, and context when a concrete operational name is available.
- Keep necessary technical vocabulary at system boundaries, but translate it into domain language in the core workflow.
Refactor safely
When refactoring existing code:
- State the behavior and public interfaces that must remain unchanged.
- Sketch the intended top-level flow before moving code.
- Make the smallest coherent change that improves readability.
- Preserve compatibility entry points when callers still depend on them.
- Run focused tests first, followed by the relevant broader test suite.
- Report behavior changes separately from structural improvements.
Do not rename public interfaces, add dependencies, or change behavior merely to make the internal design look cleaner.
Review the result
Before finishing, verify that:
- the primary workflow is visible near the top of the module;
- every extracted helper names a meaningful operation;
- related helpers are close to their callers;
- classes represent real state or lifecycle;
- names can be understood without knowing internal jargon;
- required inputs use clear positional arguments and ambiguous values use keywords;
- docstrings are brief, useful, and absent when the code is already self-explanatory;
- exceptions explain the failed operation and useful context;
- comments explain intent or constraints rather than restating code;
- tests cover the preserved behavior and any intentional change.
1---2name: readable-python3description: Design, implement, refactor, or review Python modules for explicit operational flow, descriptive naming, nearby helpers, small stateful classes, and minimal abstraction. Use when readability and simplicity are the primary code-design goals. Do not apply these conventions to non-Python code unless requested.4---56# Readable Python78Write Python so a reader can understand the main operation from top to bottom without tracing layers of indirection. Preserve the repository's established behavior and public interfaces unless the user asks to change them.910## Design the module11121. Identify the module's inputs, processing steps, outputs, state, and public API.132. Express the primary operation as a short, visible workflow.143. Extract a helper only when the extracted operation has one clear purpose and a descriptive name makes the caller easier to read.154. Keep a new helper immediately below its caller. Move it only when multiple operations genuinely share it.165. Prefer a small amount of obvious duplication over an abstraction that hides behavior.176. Split a module only when the resulting module owns a cohesive responsibility.1819## Write functions as simple machines2021- Treat each function as: typed input, visible processing, typed output.22- Give each function one meaningful operational responsibility. Do not reduce functions to arbitrary line-count targets.23- Keep decisions and side effects apparent in the main workflow.24- Pass required values explicitly instead of relying on hidden mutable state.25- Prefer positional arguments for required inputs when their meaning and order are clear from the function name and signature.26- Use keyword arguments when they clarify optional settings, booleans, repeated primitive values, or otherwise ambiguous calls.27- Avoid `*args` and `**kwargs` unless forwarding an existing compatible interface requires them.28- Return a value with a clear meaning. Avoid modes controlled by several boolean flags.29- Use early returns when they make exit conditions easier to see.3031## Write only useful docstrings3233- Omit a function or method docstring when its name, typed signature, and implementation already make its behavior clear.34- When a docstring adds value, keep it short and explain what the operation does.35- Include only material caveats, preconditions, side effects, or behavior that a reader cannot infer from the signature.36- Do not repeat parameter names, return types, or implementation steps that the code already communicates.3738## Use classes only for state or lifecycle3940- Introduce a class when an operation owns persistent state, configuration, or a resource lifecycle.41- Keep `__init__` limited to validating and storing dependencies or configuration. Do not perform network, file, or browser operations in it.42- Expose a small set of public methods named after real operations.43- Keep private helper methods rare. If a helper does not need instance state, make it a nearby module-level function.44- Do not add factories, managers, service layers, base classes, or strategy objects before the code has a concrete need for them.4546## Choose operational names4748- Prefer names that describe what the code does in the domain: `capture_browser_observation`, `choose_discovery_action`, or `store_successful_correction`.49- Name functions with a verb and the object or result of the operation.50- Name booleans as questions or conditions, such as `is_registration_page` or `has_visible_captcha`.51- Use plural names for collections and include units in values such as `timeout_seconds`.52- Avoid vague names such as `process`, `handle`, `manage`, `execute`, `data`, `item`, `object`, and `context` when a concrete operational name is available.53- Keep necessary technical vocabulary at system boundaries, but translate it into domain language in the core workflow.5455## Refactor safely5657When refactoring existing code:58591. State the behavior and public interfaces that must remain unchanged.602. Sketch the intended top-level flow before moving code.613. Make the smallest coherent change that improves readability.624. Preserve compatibility entry points when callers still depend on them.635. Run focused tests first, followed by the relevant broader test suite.646. Report behavior changes separately from structural improvements.6566Do not rename public interfaces, add dependencies, or change behavior merely to make the internal design look cleaner.6768## Review the result6970Before finishing, verify that:7172- the primary workflow is visible near the top of the module;73- every extracted helper names a meaningful operation;74- related helpers are close to their callers;75- classes represent real state or lifecycle;76- names can be understood without knowing internal jargon;77- required inputs use clear positional arguments and ambiguous values use keywords;78- docstrings are brief, useful, and absent when the code is already self-explanatory;79- exceptions explain the failed operation and useful context;80- comments explain intent or constraints rather than restating code;81- tests cover the preserved behavior and any intentional change.