| name | python-best-practices |
| description | Python development best practices including PEP 8 style guidelines, type hints, docstring conventions, and common patterns. Use when writing or modifying Python code. |
Python Best Practices
PYTHON_MASTERY::[PEP8_COMPLIANCE+TYPE_HINTS+NUMPY_DOCSTRINGS+MODERN_PATTERNS]→PRODUCTION_QUALITY
STYLE GUIDELINES (PEP 8)
PEP8_RULES::[
indentation::4_spaces_per_level,
line_length::79_chars_code[72_docstrings],
blank_lines::2_between_top_level[1_between_methods],
imports::top_of_file[stdlib→third_party→local],
naming::[
snake_case→functions+variables+modules,
PascalCase→classes,
UPPER_SNAKE_CASE→constants,
_leading_underscore→internal/private
]
]
IMPORT_ORGANIZATION::
# 1. Standard library
import os, sys
from pathlib import Path
# 2. Third-party
import requests, numpy as np
# 3. Local
from myapp.core import MyClass
CIRCULAR_IMPORTS::
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from myapp.other import OtherClass # Type hints only
TYPE HINTS (CRITICAL)
TYPE_HINT_MANDATE::[
ALWAYS::function_signatures[params+return_types],
MODERN_SYNTAX::use_|_not_Union[Python_3.10+],
GENERIC_BUILTIN::list[str]_not_List[str][Python_3.9+],
VARIABLES::annotate_complex_types[dict[str, list[int]]]
]
EXAMPLE_TYPED_FUNCTION::
def process_data(
items: list[str],
max_count: int | None = None,
verbose: bool = False
) -> dict[str, int]:
"""Process items and return counts.
Parameters
----------
items : list[str]
List of items to process
max_count : int | None, optional
Maximum items to process (default: None)
verbose : bool, optional
Enable verbose output (default: False)
Returns
-------
dict[str, int]
Dictionary mapping items to counts
"""
result: dict[str, int] = {}
for item in items[:max_count]:
result[item] = result.get(item, 0) + 1
if verbose:
print(f"Processed: {item}")
return result