Loading...
Loading...
Python coding standards and best practices. Applicable to code generation, code review, and code refactoring scenarios.
npx skill4agent add jasong98/code-skills python-coding| Type | Style | Example |
|---|---|---|
| Module/Function/Variable | | |
| Class Name | | |
| Constant | | |
| Private Attribute | | |
| Boolean Variable | Prefix with | |
# Use modern syntax (Python 3.10+)
def process(items: list[str]) -> dict[str, int]: ...
def get_user(id: int) -> User | None: ...
# Use Protocol for structural typing
from typing import Protocol
class Drawable(Protocol):
def draw(self) -> None: ...# Use dataclass when there are too many parameters
@dataclass
class EmailConfig:
to: str
subject: str
body: str
cc: list[str] = field(default_factory=list)
def send_email(config: EmailConfig) -> None: ...# Group order, sorted alphabetically within each group
import os
import sys
from pathlib import Path
import requests
from dotenv import load_dotenv
from myapp.models import User
from myapp.utils import loggerfrom module import *# Use specific exception types
try:
result = data["key"]
except KeyError:
result = default_value
except requests.Timeout:
logger.warning("Request timed out")
raise
# Prohibit bare except
# Bad: except Exception: passdef load_config(path: Path) -> dict[str, Any]:
"""加载并校验配置文件.
该函数读取配置文件并返回解析结果, 不修改全局状态.
Args:
path: 配置文件路径.
Returns:
解析后的配置字典.
Raises:
ValueError: 当配置不合法时抛出.
""". , :. , ;使用 LRU 缓存# 使用贪心策略, 因为回溯会导致指数级复杂度
result = greedy_search(data)()\except Exception: pass