Help us improve
Share bugs, ideas, or general feedback.
From python-patterns
This skill should be used when the user asks to "write idiomatic Python", "review Python code", "refactor Python", "follow PEP 8", "use type hints", "write Pythonic code", or when writing, reviewing, designing, or refactoring Python applications. Provides patterns for readability, correctness, and maintainability.
npx claudepluginhub grailautomation/claude-plugins --plugin python-patternsHow this skill is triggered — by the user, by Claude, or both
Slash command
/python-patterns:python-patternsThe summary Claude sees in its skill listing — used to decide when to auto-load this skill
Idiomatic Python patterns and best practices for building robust, efficient, and maintainable applications.
Provides Pythonic idioms, PEP 8 standards, type hints, and best practices for writing new code, reviewing, refactoring, and designing Python packages or modules.
Guides Python development with idiomatic patterns, PEP 8 standards, type hints, and best practices for readable, maintainable code.
Creates p5.js generative art with seeded randomness, noise fields, and interactive parameter exploration. Use for algorithmic art, flow fields, or particle systems.
Share bugs, ideas, or general feedback.
Idiomatic Python patterns and best practices for building robust, efficient, and maintainable applications.
Prioritize clarity. Code should be obvious to the next reader.
# Good: Clear and readable
def get_active_users(users: list[User]) -> list[User]:
return [user for user in users if user.is_active]
# Bad: Clever but confusing
def get_active_users(u):
return [x for x in u if x.a]
Avoid magic; make intent clear.
# Good: Explicit configuration
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
# Bad: Hidden side effects
some_module.setup() # What does this do?
Prefer exception handling over precondition checks.
# Good: EAFP style
try:
return dictionary[key]
except KeyError:
return default_value
# Bad: LBYL (Look Before You Leap)
if key in dictionary:
return dictionary[key]
return default_value
| Idiom | Description |
|---|---|
| EAFP | Easier to ask forgiveness than permission |
| Context managers | Use with for resource management |
| List comprehensions | For simple transformations |
| Generators | For lazy evaluation and large datasets |
| Type hints | Annotate function signatures |
| Dataclasses | For data containers with auto-generated methods |
__slots__ | For memory optimization |
| f-strings | For string formatting (Python 3.6+) |
pathlib.Path | For path manipulation (Python 3.4+) |
enumerate | For index-element pairs in loops |
# Bad: Mutable default arguments
def append_to(item, items=[]): # Shared across calls!
items.append(item)
return items
# Good: Use None sentinel
def append_to(item, items=None):
if items is None:
items = []
items.append(item)
return items
# Bad: Bare except
try:
risky_operation()
except:
pass # Silent failure!
# Good: Specific exception
try:
risky_operation()
except SpecificError as e:
logger.error(f"Operation failed: {e}")
# Bad: type() for type checking # Bad: Comparing to None with ==
if type(obj) == list: ... if value == None: ...
# Good: isinstance / identity # Good: Use is
if isinstance(obj, list): ... if value is None: ...
# Bad: Wildcard imports
from os.path import *
# Good: Explicit imports
from os.path import join, exists
Detailed patterns and examples for each topic:
Remember: Python code should be readable, explicit, and follow the principle of least surprise. When in doubt, prefer clarity over cleverness.