Development v1.0.0

python-mutable-default-args

A Python function uses a mutable object (list, dict, set) as a default argument, sharing state across calls in a way...

mvogt99 更新于 2026-04-28 1 浏览
下载 SKILL

技能说明

A Python function uses a mutable object (list, dict, set) as a default argument, sharing state across calls in a way that produces silent bugs.

使用场景

Python evaluates default argument values once at function definition time, not on each call. If the default is a mutable object — a list, dict, or set — that object is shared across every call that uses the default. Mutating it inside the function modifies the default for all future calls. The bug is invisible until the second or later call and produces state-dependent failures that are hard to trace.

直接复制以下提示词,发送给你的 AI 助手即可完成安装。

帮我下载并安装这个SKILL:https://skillhub.cstcloud.cn/download/python-mutable-default-args
  1. 点击上方「下载 SKILL」按钮,获取技能压缩包。
  2. 将压缩包解压到你的 Agent 技能目录。
  3. 重启客户端后即可在技能列表中选择该技能。

技能文件内容

4 个文件 · 14 kB
SKILL.md text/markdown
---
name: python-mutable-default-args
description: A Python function uses a mutable object (list, dict, set) as a default argument, sharing state across calls in a way that produces silent bugs.
emoji: 🐍
metadata:
  clawdis:
    os: [macos, linux, windows]
    language: python
---

# python-mutable-default-args

Python evaluates default argument values once at function definition time, not on each call. If the default is a mutable object — a list, dict, or set — that object is shared across every call that uses the default. Mutating it inside the function modifies the default for all future calls. The bug is invisible until the second or later call and produces state-dependent failures that are hard to trace.

## Symptoms

```python
def add_item(item, items=[]):   # ← shared list
    items.append(item)
    return items

add_item("a")  # ["a"]
add_item("b")  # ["a", "b"]  ← unexpected; second caller sees first caller's data
```

- A function that accumulates data into a list or dict default grows unboundedly across calls.
- A function that should be stateless produces different results on the second call than the first.
- Unit tests pass in isolation but fail when run together due to shared state from a prior test.
- The bug disappears if the function is called with an explicit argument.

## What to do

- Use `None` as the default and initialize the mutable inside the function body:

  ```python
  def add_item(item, items=None):
      if items is None:
          items = []
      items.append(item)
      return items
  ```

- This is the canonical Python idiom. Every call that omits `items` gets a fresh list.
- Applies equally to `dict`, `set`, and any other mutable type. The rule is: never use a mutable as a default argument.
- When reviewing code, scan every function signature for `=[]`, `={}`, `=set()` as a heuristic for this pattern.
- Linters (pylint rule `W0102`, ruff rule `B006`) will flag this automatically — enable them if the codebase doesn't already.

文件内容直接读取自技能仓库,仅供安装前审阅;以实际下载的压缩包为准。