Loading...
Loading...
Compare original and translation side by side
┌──────────────────────────────────────────────────────────┐
│ FRIENDLY PYTHON = User-Friendly Python │
├────────────────────────────────┬─────────────────────────┤
│ User-Friendly │ Maintainer-Friendly │
│ ───────────────── │ ───────────────── │
│ • Sensible defaults │ • Single change point │
│ • Minimal required params │ • Registry over if-else│
│ • Hidden resource mgmt │ • Explicit over magic │
│ • Simple → complex path │ • Readable & debuggable│
└────────────────────────────────┴─────────────────────────┘┌──────────────────────────────────────────────────────────┐
│ FRIENDLY PYTHON = User-Friendly Python │
├────────────────────────────────┬─────────────────────────┤
│ User-Friendly │ Maintainer-Friendly │
│ ───────────────── │ ───────────────── │
│ • Sensible defaults │ • Single change point │
│ • Minimal required params │ • Registry over if-else│
│ • Hidden resource mgmt │ • Explicit over magic │
│ • Simple → complex path │ • Readable & debuggable│
└────────────────────────────────┴─────────────────────────┘classmethod__init__classmethod__init____getattr__┌─────────────────────────────────────────────────────────────┐
│ 1. UNDERSTAND: 理解需求与现有代码 │
├─────────────────────────────────────────────────────────────┤
│ 2. ANALYZE: 分析代码风格问题,识别非 Pythonic 模式 │
├─────────────────────────────────────────────────────────────┤
│ 3. IMPROVE: 应用 Pythonic 惯用法和设计模式 │
├─────────────────────────────────────────────────────────────┤
│ 4. REVIEW: 根据审查清单检查改进方案 │
├─────────────────────────────────────────────────────────────┤
│ 5. REFINE: 优化细节,提供替代方案和权衡分析 │
└─────────────────────────────────────────────────────────────┘┌─────────────────────────────────────────────────────────────┐
│ 1. UNDERSTAND: Understand requirements and existing code │
├─────────────────────────────────────────────────────────────┤
│ 2. ANALYZE: Analyze code style issues and identify non-Pythonic patterns │
├─────────────────────────────────────────────────────────────┤
│ 3. IMPROVE: Apply Pythonic idioms and design patterns │
├─────────────────────────────────────────────────────────────┤
│ 4. REVIEW: Check improvement plans against review checklists │
├─────────────────────────────────────────────────────────────┤
│ 5. REFINE: Optimize details and provide alternative solutions and trade-off analysis │
└─────────────────────────────────────────────────────────────┘| 检查项 | 问题 |
|---|---|
| 🔧 扩展性 | 新增功能是否只需修改一处代码? |
| 🎯 默认值 | API 是否有合理的默认值?是否隐藏了不必要的对象? |
| 📈 复杂度 | 复杂度是否遵循"简单到复杂",默认路径是否最轻量? |
| 🔌 扩展点 | 是否优先使用生态系统的扩展点? |
| 👁️ 明确性 | 是否为了炫技而牺牲了明确性和可维护性? |
| 🔄 移植代码 | 移植代码时是否重新设计了调用模式? |
| Check Item | Question |
|---|---|
| 🔧 Extensibility | Does adding new features only require modifying one place in the code? |
| 🎯 Default Values | Does the API have reasonable default values? Are unnecessary objects hidden? |
| 📈 Complexity | Does complexity follow "simple to complex", and is the default path the lightest? |
| 🔌 Extension Points | Are ecosystem extension points prioritized? |
| 👁️ Explicitness | Is explicitness and maintainability sacrificed for showing off skills? |
| 🔄 Ported Code | Was the calling pattern redesigned when porting code? |
| 场景 | 推荐做法 |
|---|---|
| 多种实现方式 | 注册表模式 + 装饰器注册 |
| 资源管理 | 上下文管理器 ( |
| 多种输入来源 | |
| 配置字段 | 描述符 (Descriptor) |
| 扩展第三方库 | 官方扩展点 (hook/adapter/auth) |
| 异步操作 | async/await + try/except/finally |
| CLI 工具 | argparse + Command 类 |
| 复杂对象构建 | Builder 模式 |
| 策略选择 | 注册表/字典查找 |
| 循环导入 | 延迟导入/重构模块 |
| Scenario | Recommended Practice |
|---|---|
| Multiple implementation methods | Registry pattern + decorator registration |
| Resource management | Context manager ( |
| Multiple input sources | |
| Configuration fields | Descriptors |
| Extending third-party libraries | Official extension points (hook/adapter/auth) |
| Asynchronous operations | async/await + try/except/finally |
| CLI tools | argparse + Command class |
| Complex object construction | Builder pattern |
| Strategy selection | Registry/dictionary lookup |
| Circular imports | Lazy import/refactor modules |
| 反模式 | 问题 |
|---|---|
| 大量 if-else 分支 | 添加功能需要修改多处 |
| 互斥参数不明确 |
| 削弱可发现性和类型检查 |
| 过度使用元类 | 污染用户的心智模型 |
| 自定义包装回原库 | 属性重复,维护负担 |
| JS 风格的回调 | 不 Pythonic |
| 全局状态 | 难以测试和维护 |
| 过度继承 | 组合优于继承 |
| 硬编码配置 | 缺乏灵活性 |
| 裸 except | 吞掉所有异常 |
| Anti-Pattern | Problem |
|---|---|
| A large number of if-else branches | Adding features requires modifying multiple places |
Using flags to control paths in | Mutually exclusive parameters are unclear |
| Weakens discoverability and type checking |
| Overuse of metaclasses | Pollutes users' mental models |
| Custom wrapping of original libraries | Duplicate attributes, maintenance burden |
| JS-style callbacks | Not Pythonic |
| Global state | Difficult to test and maintain |
| Over-inheritance | Composition over inheritance |
| Hard-coded configuration | Lack of flexibility |
| Bare except | Swallows all exceptions |
undefinedundefinedundefinedundefinedassets/templates/assets/templates/