bug stringclasses 3
values | title stringclasses 3
values | broken_code stringclasses 3
values | fixed_code stringclasses 3
values | what_went_wrong stringclasses 3
values | fix stringclasses 3
values |
|---|---|---|---|---|---|
mutable-default-arg | Mutable default argument | """BROKEN: a mutable default argument is shared between calls.
The default list [] is created ONCE, when the function is defined —
not fresh on every call. Every call that appends to it changes the
same list, so items from earlier calls "leak" into later ones.
Run it: each call should start with an empty list, but it... | """FIXED: use None as the default and create a fresh list inside.
The bug: a mutable default argument (like []) is created once, when the
function is defined, and shared between all calls. The fix: default to
None, then build a new list on every call.
"""
def add_item(item, items=None):
if items is None:
... | 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. | Default to None and create a fresh list inside the function on every call. |
late-binding-closures | Late binding closures | """BROKEN: lambdas created in a loop share the loop variable.
The lambda does NOT capture the value of i from its own round of the
loop — it looks up i when it is CALLED. By then the loop is finished
and i is 2 (the last value), so every function returns 2.
Run it: all three should print 0, 1, 2 — but all print 2.
""... | """FIXED: bind the current value with a default argument.
The bug: a lambda (or any closure) 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. The fix: capture the value now with a default
argument (i=i), which is evaluated when the lambda i... | 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. | Capture the value with a default argument (i=i), which is evaluated when the lambda is defined. |
modifying-list-while-iterating | Modifying a list while iterating | """BROKEN: deleting from a list while iterating over it skips items.
When you remove an item, the list shifts left, but the loop still
moves to the next index — so the element that slid into the removed
slot is never checked.
Run it: every number is even, so the result should be an empty list —
but 4 and 8 survive, b... | """FIXED: iterate over a copy, or build a new list.
The bug: 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. The fix: loop over a copy (numbers[:]) and delete
from the original — or, cleaner, build a new list with a comprehens... | 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. | Iterate over a copy (numbers[:]) and delete from the original, or build a new list with a comprehension. |
Python Bug Fixes
Common Python bugs fixed and explained — real broken scripts with before and after code and what went wrong.
Source: Battingchart300/python-bug-fixes on GitHub (MIT).
Data
One record per bug, in data.jsonl:
| Field | What it is |
|---|---|
bug |
Short slug, e.g. mutable-default-arg |
title |
Human-readable bug name |
broken_code |
The buggy version — run it and see the bug happen |
fixed_code |
The fixed version — run it and see the correct result |
what_went_wrong |
Plain-English explanation of the bug |
fix |
Plain-English explanation of the fix |
Bugs
| Bug | In short |
|---|---|
mutable-default-arg |
def f(x=[]) shares one list between all calls |
late-binding-closures |
A lambda in a loop sees the last loop value, not the one from its own round |
modifying-list-while-iterating |
Deleting from a list while looping over it skips elements |
Use
from datasets import load_dataset
ds = load_dataset("Battingchart300/python-bug-fixes")
print(ds["train"][0]["title"])
print(ds["train"][0]["broken_code"])
License
MIT
- Downloads last month
- 17