微算力 · API 导航与自营网关
API 接入 EN
未登录登录
🐍

规避 Python 陷阱的权威指南

python · vlatest
入口已确认,内容待校验 文件树里有 SKILL.md,但正文还没采回,frontmatter 未核对。
开发 安全 S+ 级 作者 Unknown 公开目录收录
2.1k 下载 473 安装 9k 收藏 99.4k 浏览

简介

源自社区最佳实践的 Python 避坑指南,系统梳理可变参数、导入陷阱等运行时隐患,助力开发者编写健壮代码。

原始描述

Write reliable Python avoiding mutable defaults, import traps, and common runtime surprises.

通过你的 Agent 添加

安装目录:python-5796/(目录名取自 SKILL.md 的 frontmatter name,不是 URL 里的 slug)。

点「快速安装」复制一段指令贴给你的 Agent:它会先下载到 /tmp、把文件清单和校验值给你看,你确认之后才解压安装。不会一步装完、也不改系统其它位置。

或者手动安装

Claude Code~/.claude/skills/python-5796/
Codex CLI~/.codex/skills/python-5796/
DeepSeek Harness~/.agents/skills/python-5796/

包是「根级 SKILL.md + 作者附带的文件」,解压到上面的 <slug>/ 目录即可; 目录名不用换,也不要把包直接解压到 skills 根目录。 技能申请的权限由作者决定:装之前先看清单里有没有脚本、它会读写什么。

技能正文


name: Python description: Write reliable Python avoiding mutable defaults, import traps, and common runtime surprises. metadata: {"clawdbot":{"emoji":"🐍","requires":{"bins":["python3"]},"os":["linux","darwin","win32"]}}

Mutable Default Arguments

  • def f(items=[]) shares list across all calls — use items=None then items = items or []
  • Same for dicts, sets, any mutable — default evaluated once at definition, not per call
  • Class attributes with mutables shared across instances — define in __init__ instead

Import Traps

  • Circular imports fail silently or partially — import inside function to break cycle
  • from module import * pollutes namespace — explicit imports always
  • Relative imports require package context — python -m package.module not python module.py
  • Import order matters for monkey patching — patch before target module imports patched module
  • __init__.py runs on package import — keep it minimal

Scope Gotchas

  • UnboundLocalError when assigning to variable that exists in outer scope — use nonlocal or global
  • List comprehension has own scope, but loop variable leaks in Python 2 — not 3
  • except Exception as e — e is deleted after except block in Python 3
  • Default argument values bound at definition — closure over loop variable captures final value

String Pitfalls

  • is vs ==: is checks identity, == checks equality — "a" * 100 is "a" * 100 may be False
  • f-strings evaluate at runtime — f"{obj}" calls __str__ each time
  • str.split() vs str.split(' ') — no arg splits on any whitespace and removes empties
  • Unicode normalization: 'café' != 'café' if composed differently — use unicodedata.normalize()

Iteration Traps

  • Modifying list while iterating skips elements — iterate over copy: for x in list(items):
  • Dictionary size change during iteration raises RuntimeError — copy keys: for k in list(d.keys()):
  • Generator exhausted after one iteration — can't reuse, recreate or use itertools.tee
  • range() doesn't include end — range(5) is 0,1,2,3,4

Class Gotchas

  • __init__ is not constructor — __new__ creates instance, __init__ initializes
  • Method resolution order (MRO) in multiple inheritance — use super() correctly or break chain
  • @property makes attribute read-only unless you add setter
  • __slots__ breaks __dict__ and dynamic attributes — can't add new attributes
  • Mutable class attribute shared by all instances — each instance modifying same list

Exception Handling

  • Bare except: catches SystemExit and KeyboardInterrupt — use except Exception:
  • except A, B: is syntax error in Python 3 — use except (A, B):
  • Exception chaining: raise NewError() from original preserves context
  • finally runs even on return — return in finally overrides try's return
  • else in try/except runs only if no exception — often clearer than flag variable

Numeric Surprises

  • 0.1 + 0.2 != 0.3 — floating point, use decimal.Decimal for money
  • Integer division: // always floors toward negative infinity — -7 // 2 is -4, not -3
  • bool is subclass of int — True + True == 2
  • Large integers have no overflow — but operations get slow
  • is fails for large integers — only small ints (-5 to 256) are cached

File and I/O

  • open() without context manager leaks file handles — always use with open():
  • Default encoding is platform-dependent — always specify encoding='utf-8'
  • read() loads entire file to memory — use readline() or iterate for large files
  • Binary mode 'rb' required for non-text — Windows line endings differ in text mode

Concurrency

  • GIL prevents true parallel Python threads — use multiprocessing for CPU-bound
  • Threading still useful for I/O-bound — network, file operations release GIL
  • async/await is not threading — single-threaded cooperative multitasking
  • multiprocessing shares nothing by default — use Queue or Manager for communication
  • Daemon threads killed abruptly on exit — may corrupt data

正文来自原始发布页, 版权归作者所有,许可未标注。 权利人认为有不妥,可申请下架。

标签

development-engineeringeducation-researchbackenddocspython

同类 Skill