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. |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.