Python Mutable Default Args

v1.0.0

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.

0· 38·0 current·0 all-time

Install

OpenClaw Prompt Flow

Install with OpenClaw

Best for remote or guided setup. Copy the exact prompt, then paste it into OpenClaw for mvogt99/python-mutable-default-args.

Previewing Install & Setup.
Prompt PreviewInstall & Setup
Install the skill "Python Mutable Default Args" (mvogt99/python-mutable-default-args) from ClawHub.
Skill page: https://clawhub.ai/mvogt99/python-mutable-default-args
Keep the work scoped to this skill only.
After install, inspect the skill metadata and help me finish setup.
Use only the metadata you can verify from ClawHub; do not invent missing requirements.
Ask before making any broader environment changes.

Command Line

CLI Commands

Use the direct CLI path if you want to install manually and keep every step visible.

OpenClaw CLI

Bare skill slug

openclaw skills install python-mutable-default-args

ClawHub CLI

Package manager switcher

npx clawhub@latest install python-mutable-default-args
Security Scan
VirusTotalVirusTotal
Benign
View report →
OpenClawOpenClaw
Benign
high confidence
Purpose & Capability
Name and description match the SKILL.md content. The skill only explains a common Python pitfall and remediation; it requests no binaries, credentials, or config that would be unrelated to that purpose.
Instruction Scope
Instructions are limited to explaining the bug, showing safe code patterns, suggesting heuristics for searching code (signatures like '=[]', '={}', '=set()'), and recommending linters (pylint/ruff). The skill does not instruct the agent to read system files, access environment variables, or transmit data externally.
Install Mechanism
No install spec or code files are present (instruction-only). Nothing will be written to disk or executed by an installer as part of installing this skill.
Credentials
The skill requests no environment variables, credentials, or config paths. No sensitive access is required to accomplish the stated purpose.
Persistence & Privilege
always is false and the skill is user-invocable; autonomous invocation is allowed (platform default) but the skill's instructions are benign and do not require elevated or persistent system privileges.
Assessment
This skill is a harmless, text-only explanation of a Python coding pitfall and how to fix it. It requires no install, no credentials, and does not instruct the agent to read or transmit files. You can safely enable or use it to get guidance on mutable default arguments. If you plan to act on its suggestions, consider enabling linters (pylint/ruff) and running a repository-wide search for patterns like '=[]', '={}', and '=set()' to find instances to fix. If you want stricter assurance, review the SKILL.md yourself — it contains all runtime instructions and performs no external actions.

Like a lobster shell, security has layers — review code before you run it.

Runtime requirements

OSmacOS · Linux · Windows
latestvk9767h78c3z1rjkj2jkb4233qn85pv3w
38downloads
0stars
1versions
Updated 6h ago
v1.0.0
MIT-0
macOS, Linux, Windows

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

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:

    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.

Comments

Loading comments...