Battingchart300 commited on
Commit
90bddf2
·
verified ·
1 Parent(s): fe1a549

Upload 2 files

Browse files
Files changed (2) hide show
  1. README.md +50 -0
  2. data.jsonl +3 -0
README.md CHANGED
@@ -1,3 +1,53 @@
1
  ---
2
  license: mit
 
 
 
 
 
 
 
 
 
3
  ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
  license: mit
3
+ language:
4
+ - en
5
+ pretty_name: Python Bug Fixes
6
+ size_categories:
7
+ - n<1K
8
+ tags:
9
+ - code
10
+ - python
11
+ - debugging
12
  ---
13
+
14
+ # Python Bug Fixes
15
+
16
+ Common Python bugs fixed and explained — real broken scripts with before and after code and what went wrong.
17
+
18
+ Source: [Battingchart300/python-bug-fixes](https://github.com/Battingchart300/python-bug-fixes) on GitHub (MIT).
19
+
20
+ ## Data
21
+
22
+ One record per bug, in `data.jsonl`:
23
+
24
+ | Field | What it is |
25
+ | --- | --- |
26
+ | `bug` | Short slug, e.g. `mutable-default-arg` |
27
+ | `title` | Human-readable bug name |
28
+ | `broken_code` | The buggy version — run it and see the bug happen |
29
+ | `fixed_code` | The fixed version — run it and see the correct result |
30
+ | `what_went_wrong` | Plain-English explanation of the bug |
31
+ | `fix` | Plain-English explanation of the fix |
32
+
33
+ ## Bugs
34
+
35
+ | Bug | In short |
36
+ | --- | --- |
37
+ | `mutable-default-arg` | `def f(x=[])` shares one list between all calls |
38
+ | `late-binding-closures` | A `lambda` in a loop sees the last loop value, not the one from its own round |
39
+ | `modifying-list-while-iterating` | Deleting from a list while looping over it skips elements |
40
+
41
+ ## Use
42
+
43
+ ```python
44
+ from datasets import load_dataset
45
+
46
+ ds = load_dataset("Battingchart300/python-bug-fixes")
47
+ print(ds["train"][0]["title"])
48
+ print(ds["train"][0]["broken_code"])
49
+ ```
50
+
51
+ ## License
52
+
53
+ MIT
data.jsonl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ {"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."}
2
+ {"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."}
3
+ {"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."}