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 that produces silent bugs.

Safety Notice

This listing is from the official public ClawHub registry. Review SKILL.md and referenced scripts before running.

Copy this and send it to your AI assistant to learn

Install skill "python-mutable-default-args" with this command: npx skills add mvogt99/python-mutable-default-args

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.

Source Transparency

This detail page is rendered from real SKILL.md content. Trust labels are metadata-based hints, not a safety guarantee.

Related Skills

Related by shared tags or category signals.

Coding

Video Game

Get highlight reel clips ready to post, without touching a single slider. Upload your gameplay footage (MP4, MOV, AVI, WebM, up to 500MB), say something like...

Registry SourceRecently Updated
Coding

Bed Bath Beyond

Provides detailed insights into Bed Bath & Beyond's rise, coupon-driven decline, e-commerce challenges, and 2023 bankruptcy for retail and marketing research.

Registry SourceRecently Updated
Coding

Editor Kiss

Turn a 3-minute interview recording with pauses and filler words into 1080p kiss-cut edited clips just by typing what you need. Whether it's removing pauses...

Registry SourceRecently Updated
Coding

Maker Hindi

Turn a 60-second product clip or five product images into 1080p Hindi language videos just by typing what you need. Whether it's creating videos with Hindi t...

Registry SourceRecently Updated