Python production code patterns and anti-patterns. Use when writing Python code.
Instalação
Instalar com Codex ou Claude Copie este prompt, cole no Codex, Claude ou outro assistente e deixe que ele revise a página da skill e instale para você.
Only re-export what downstream users actually need
Keep __init__.py minimal — imports and __all__, no logic
Import Patterns
Lazy imports only when measured startup time justifies it. Don't guess — profile first.
Standard import order: stdlib, third-party, local (blank line between groups)
No wildcard imports (from module import *)
Subprocess Calls
Always use subprocess.run() with explicit args list:
# BAD
subprocess.run(f"cmd {user_input}", shell=True)
# GOOD
result = subprocess.run(
["cmd", user_input],
capture_output=True,
text=True,
check=True,
)
Use check=True to raise on non-zero exit codes (or handle returncode explicitly)
Exception Handling
Define a project-level base exception, inherit specific exceptions from it:
classAppError(Exception):
"""Base exception for this project."""classConfigError(AppError):
"""Invalid configuration."""classDataLoadError(AppError):
"""Failed to load data from source."""
Never bare except: — always catch specific exceptions
Don't silence exceptions without logging:
# BADtry:
process(data)
except Exception:
pass# ACCEPTABLE (when you genuinely expect and handle the case)try:
value = cache[key]
except KeyError:
value = compute_value(key)
Design Principles
Composition over inheritance: inject dependencies, don't subclass for code reuse
Functions over classes for stateless operations: if your class has no __init__ state and one method, it should be a function
Use enum.Enum for fixed sets, not string constants:
# BAD
mode = "read"# typo-prone, no IDE completion# GOODclassMode(enum.Enum):
READ = "read"
WRITE = "write"
String Formatting
f-strings for simple interpolation: f"Hello, {name}"
.format() only when the template is stored separately
Never use % formatting in new code
Type Annotations
Required on public API signatures (functions, methods, class attributes exposed in __all__)
Don't over-annotate private helpers — type checkers can infer most local types
Use from __future__ import annotations for forward references (Python 3.9 compatibility)