{"bug": "mutable-default-arg", "title": "Mutable default argument", "broken_code": "\"\"\"BROKEN: a mutable default argument is shared between calls.\n\nThe default list [] is created ONCE, when the function is defined —\nnot fresh on every call. Every call that appends to it changes the\nsame list, so items from earlier calls \"leak\" into later ones.\n\nRun it: each call should start with an empty list, but it doesn't.\n\"\"\"\n\n\ndef add_item(item, items=[]):\n items.append(item)\n return items\n\n\nprint(add_item(\"apple\")) # expected: ['apple']\nprint(add_item(\"banana\")) # expected: ['banana'] — but gets ['apple', 'banana']\nprint(add_item(\"cherry\")) # expected: ['cherry'] — but gets all three\n", "fixed_code": "\"\"\"FIXED: use None as the default and create a fresh list inside.\n\nThe bug: a mutable default argument (like []) is created once, when the\nfunction is defined, and shared between all calls. The fix: default to\nNone, then build a new list on every call.\n\"\"\"\n\n\ndef add_item(item, items=None):\n if items is None:\n items = []\n items.append(item)\n return items\n\n\nprint(add_item(\"apple\")) # ['apple']\nprint(add_item(\"banana\")) # ['banana']\nprint(add_item(\"cherry\")) # ['cherry']\n", "what_went_wrong": "A mutable default argument (like []) is created once, when the function is defined, and shared between all calls, so items from earlier calls leak into later ones.", "fix": "Default to None and create a fresh list inside the function on every call."} {"bug": "late-binding-closures", "title": "Late binding closures", "broken_code": "\"\"\"BROKEN: lambdas created in a loop share the loop variable.\n\nThe lambda does NOT capture the value of i from its own round of the\nloop — it looks up i when it is CALLED. By then the loop is finished\nand i is 2 (the last value), so every function returns 2.\n\nRun it: all three should print 0, 1, 2 — but all print 2.\n\"\"\"\n\nfunctions = []\nfor i in range(3):\n functions.append(lambda: i)\n\nfor f in functions:\n print(f()) # expected: 0, 1, 2 — actual: 2, 2, 2\n", "fixed_code": "\"\"\"FIXED: bind the current value with a default argument.\n\nThe bug: a lambda (or any closure) created in a loop looks up the loop\nvariable when it is called, not when it is created — so every function\nsees the last value. The fix: capture the value now with a default\nargument (i=i), which is evaluated when the lambda is defined.\n\"\"\"\n\nfunctions = []\nfor i in range(3):\n functions.append(lambda i=i: i)\n\nfor f in functions:\n print(f()) # 0, 1, 2\n", "what_went_wrong": "A lambda created in a loop looks up the loop variable when it is called, not when it is created, so every function sees the last value of the loop.", "fix": "Capture the value with a default argument (i=i), which is evaluated when the lambda is defined."} {"bug": "modifying-list-while-iterating", "title": "Modifying a list while iterating", "broken_code": "\"\"\"BROKEN: deleting from a list while iterating over it skips items.\n\nWhen you remove an item, the list shifts left, but the loop still\nmoves to the next index — so the element that slid into the removed\nslot is never checked.\n\nRun it: every number is even, so the result should be an empty list —\nbut 4 and 8 survive, because they were skipped.\n\"\"\"\n\nnumbers = [2, 4, 6, 8]\nfor n in numbers:\n if n % 2 == 0:\n numbers.remove(n)\n\nprint(numbers) # expected: [] — actual: [4, 8]\n", "fixed_code": "\"\"\"FIXED: iterate over a copy, or build a new list.\n\nThe bug: deleting from a list while iterating over it shifts the\nremaining items left, so the loop skips the element that slid into\nthe removed slot. The fix: loop over a copy (numbers[:]) and delete\nfrom the original — or, cleaner, build a new list with a comprehension.\n\"\"\"\n\nnumbers = [2, 4, 6, 8]\nfor n in numbers[:]: # iterate over a copy\n if n % 2 == 0:\n numbers.remove(n)\n\nprint(numbers) # []\n\n# The cleaner way: keep only what you want\nnumbers = [2, 4, 6, 8]\nnumbers = [n for n in numbers if n % 2 != 0]\nprint(numbers) # []\n", "what_went_wrong": "Deleting from a list while iterating over it shifts the remaining items left, so the loop skips the element that slid into the removed slot.", "fix": "Iterate over a copy (numbers[:]) and delete from the original, or build a new list with a comprehension."}