Python健壮编程
FreeNo executable scriptsNot checked编写可靠Python代。支持自动化配置和灵活的参数设置,适适配多种工作环境,增强工作效率。Python健壮编程工具。支持自动化配置和灵活的参数设置,适用于多种工作场景,提升工作效率和准确性。Python健壮编程是一款高效实用的工具。py支持多种配置选项。Use when…
by thcjp741 installs in other catalogsAbout this skill
核心功能: 本技能提供化流程等能力。
核心功能: 本技能提供自动化配置和灵活的参数设置、多种配置选项等能力。
Python健壮编程
编写可靠Python代码,避免可变默认值、导入陷阱与常见运行时意外。涵盖从动态类型陷阱到并发模型的完整防护指南.
输入参数
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| input | string | 是 | Python健壮编程处理的输入数据或指令 |
| options | object | 否 | 附加配置选项,如模式选择、格式偏好等 |
| callback_url | string | 否 | 异步处理完成后的回调通知URL |
专业版增强能力
| 能力 | 免费版 | 付费版 |
|---|---|---|
| 基础功能 | 支持 | 支持 |
| 代码静态分析与质量评分 | 不支持 | 支持 |
| 依赖漏洞检测与升级建议 | 不支持 | 支持 |
| 批量代码审查与报告生成 | 不支持 | 支持 |
| CI/CD流水线集成 | 不支持 | 支持 |
| 代码复杂度可视化与重构建议 | 不支持 | 支持 |
快速参考
| 主题 | 文件 |
|---|---|
| 动态类型、类型提示、鸭子类型 | types.md |
| List/dict/set陷阱、推导式 | collections.md |
| Args/kwargs、闭包、装饰器、生成器 | functions.md |
| 继承、描述符、元类 | classes.md |
| GIL、线程、asyncio、多进程 | concurrency.md |
循环导入、包、__init__.py |
imports.md |
| Pytest、模拟、fixtures | testing.md |
运行环境
运行环境
- Agent平台: 支持SKILL.md的任意AI Agent(Claude Code / Cursor / Codex / Gemini CLI等)
- 操作系统: Windows / macOS / Linux
依赖项
| 依赖项 | 类型 | 是否必需 | 获取方式 |
|---|---|---|---|
| LLM API | API | 必需 | 由Agent内置LLM提供 |
API Key 配置
需要配置对应API Key,详见上文环境配置章节
可用性分类
- 分类: MD+EXEC()
API Key配置方式:
export API_KEY="${API_KEY:?请设置环境变量}"
配置后需重启会话或开启新终端生效。API Key应妥善保管,避免泄露到版本控制系统.
能力清单
- 可变默认参数防护:
def f(items=[])在所有调用间共享同一列表,使用items=None后items = items or []创建独立实例 - 身份与相等性区分:
is检查对象身份,==检查值相等性,"a" * 100 is "a" * 100可能为False,比较值始终用== - 迭代修改安全: 迭代时修改列表会跳过元素,使用
for x in list(items):迭代副本或使用推导式创建新列表 - GIL与并发模型: GIL阻止Python线程真正并行执行CPU密集任务,使用
multiprocessing实现CPU并行,asyncio实现IO并发 - 异常捕获精确性: 裸
except:会捕获SystemExit和KeyboardInterrupt,使用except Exception:仅捕获业务异常 - 作用域与变量绑定: 函数内赋值外部变量触发
UnboundLocalError,使用nonlocal(嵌套函数)或global(模块级)声明 - 资源管理:
open()不使用上下文管理器会泄漏文件句柄,始终使用with open():确保资源释放 - 循环导入处理: 循环导入会静默失败或部分加载,在函数内部导入打破循环或将公共依赖提取到独立模块
- 浮点精度与货币计算:
0.1 + 0.2 != 0.3是浮点精度问题,货币计算使用decimal.Decimal保证精确 - 生成器与迭代器: 生成器在一次迭代后耗尽,不可复用,需重新创建或使用
itertools.tee复制迭代器 - 类属性与实例属性: 类属性中的可变对象在所有实例间共享,应在
__init__中定义实例属性 - 编码一致性: 默认编码依赖平台,文件操作始终指定
encoding='utf-8'避免跨平台问题
快速入门指南
- 确认运行环境满足依赖说明中的要求
- 在AI Agent对话中调用本技能,提供必要的输入参数
- 检查输出结果,根据需要进行后续处理
详细的输入输出格式请参考下方章节说明。
关键规则
def f(items=[])在所有调用间共享列表 — 使用items=None然后items = items or []is检查身份,==检查相等性 —"a" * 100 is "a" * 100可能为False- 迭代时修改列表会跳过元素 — 迭代副本:
for x in list(items): - GIL阻止Python线程真正并行 — CPU密集任务使用
multiprocessing - 裸
except:捕获SystemExit和KeyboardInterrupt— 使用except Exception: - 函数内赋值外部变量触发
UnboundLocalError— 使用nonlocal或global open()不使用上下文管理器会泄漏句柄 — 始终使用with open():- 循环导入会静默失败或部分加载 — 在函数内部导入打破循环
0.1 + 0.2 != 0.3— 浮点精度问题,货币计算使用decimal.Decimal- 生成器一次迭代后耗尽 — 不可复用,重新创建或使用
itertools.tee - 类属性中的可变对象在实例间共享 — 在
__init__中定义 __init__不是构造函数 —__new__创建实例,__init__初始化- 默认编码依赖平台 — 始终指定
encoding='utf-8'
操作步骤
- 检查所有函数默认参数,将可变默认值(
[]、{}、set())替换为None并在函数体内创建新实例 - 审查相等性比较,值比较使用
==而非is,is仅用于与None、True、False比较 - 检查迭代代码,迭代时需修改集合应使用
list(items)创建副本,或使用推导式/filter()生成新列表 - 评估并发需求,CPU密集型任务使用
multiprocessing.Process,IO密集型使用asyncio或threading - 审查异常处理,将裸
except:替换为except Exception:,对特定异常使用精确类型捕获 - 检查文件和资源操作,确保所有
open()使用with语句,网络连接和锁同样使用上下文管理器 - 分析导入结构,循环导入通过函数内导入或重构模块依赖解决
- 货币和精度计算使用
decimal.Decimal,文件操作指定encoding='utf-8' - 生成器不可复用,需多次遍历使用
list()转换或itertools.tee复制 - 编写Pytest测试,使用
fixtures管理测试资源,unittest.mock模拟外部依赖
结果验证: 任务完成后,查看输出确认状态。成功时返回摘要和数据;失败时根据错误信息排查,参考恢复章节获取修复步骤.
示例展示
示例1:可变默认参数陷阱
# 错误: 所有调用共享同一列表
def add_item(item, items=[]):
items.append(item)
return items
# ...
print(add_item(1)) # [1]
print(add_item(2)) # [1, 2] — 不是预期的 [2]!
# ...
# 正确: 使用 None 作为哨兵值
def add_item(item, items=None):
if items is None:
items = []
items.append(item)
return items
# ...
print(add_item(1)) # [1]
print(add_item(2)) # [2] — 正确
示例2:迭代时安全修改
# 错误: 迭代时删除元素会跳过元素
items = [1, 2, 3, 4, 5]
for item in items:
if item % 2 == 0:
items.remove(item)
print(items) # [1, 3, 5] — 跳过了4!
# ...
# 正确: 迭代副本
items = [1, 2, 3, 4, 5]
for item in list(items):
if item % 2 == 0:
items.remove(item)
print(items) # [1, 3, 5] — 正确
# ...
# 正确: 使用推导式创建新列表
items = [1, 2, 3, 4, 5]
items = [x for x in items if x % 2 != 0]
print(items) # [1, 3, 5]
示例3:浮点精度与Decimal
# 错误: 浮点数精度问题
print(0.1 + 0.2 == 0.3) # False
print(0.1 + 0.2) # 0.30000000000000004
# ...
# 正确: 货币计算使用 decimal.Decimal
from decimal import Decimal
# ...
price = Decimal('19.99')
tax_rate = Decimal('0.08')
total = price + (price * tax_rate)
print(total) # 21.5892 — 精确计算
print(total == Decimal('21.5892')) # True
示例4:生成器耗尽与复制
Install Python健壮编程 in Claude Code & Claude Desktop
Sign up to install this skill
Create a free account to reveal the install command and save the skill to your library.
- Reveal the one-line install command
- Save skills to your synced library
- Get notified when skills update
Allowed tools
Tools this skill is permitted to call.
No restriction — this skill can use any tool.
FAQ
What does the Python健壮编程 skill do?
编写可靠Python代。支持自动化配置和灵活的参数设置,适适配多种工作环境,增强工作效率。Python健壮编程工具。支持自动化配置和灵活的参数设置,适用于多种工作场景,提升工作效率和准确性。Python健壮编程是一款高效实用的工具。py支持多种配置选项。Use when 需要提升效率、自动化流程、批量处理、工作流优化时使用。不适用于需要人工创意判断的任务。 功能涵盖: toolkit。
How do I install the Python健壮编程 skill?
Copy the skill folder into ~/.claude/skills (the Claude Code tab above does this in one command), or install it as a plugin.
Does the Python健壮编程 skill run scripts?
No, this skill is instructions only (SKILL.md) with no executable scripts.
Related skills
theme-factory
Toolkit for styling artifacts with a theme. These artifacts can be slides, docs, reportings, HTML landing pages, etc. There are 10 pre-set themes with…
by Anthropicaccess
Manage Discord channel access — approve pairings, edit allowlists, set DM/group policy. Use when the user asks to pair, approve someone, check who's allowed…
by Anthropicconfigure
Set up the Discord channel — save the bot token and review access policy. Use when the user pastes a Discord bot token, asks to configure Discord, asks "how…
by Anthropicaccess
Manage iMessage channel access — approve pairings, edit allowlists, set DM/group policy. Use when the user asks to pair, approve someone, check who's allowed…
by Anthropic