1 SUSPECT #1
mod_2 = lambda x: x % 2
from operator import attrgetter
class Item:
def __init__(self, p): self.p = p
items = [Item(3), Item(1), Item(4)]
sorted_items = sorted(items, key=attrgetter("p"))
print([i.p for i in sorted_items]) # (no error)
[1, 3, 4] 2 SUSPECT #2
import json
import math
data = {"value": math.nan}
print(json.dumps(data)) # (no error in Python's default β but the OUTPUT is non-standard JSON.
# Most parsers will reject. Strict mode: json.dumps(data, allow_nan=False) raises.)
{"value": NaN} 3 SUSPECT #3
class Counter:
count = 0
def __init__(self):
Counter.count += 1
a = Counter(); b = Counter(); c = Counter()
print(Counter.count, a.count, b.count, c.count) # (no error β but the bug is conceptual: class var vs instance var)
3 3 3 3 4 SUSPECT #4
def outer():
x = "outer"
def inner():
x = "inner"
return x
inner()
return x
print(outer()) # (no error β closures don't pollute the parent scope without nonlocal)
outer 5 SUSPECT #5
import csv, io
buf = io.StringIO()
writer = csv.writer(buf)
writer.writerow(["name", "age", None])
print(buf.getvalue()) Traceback (most recent call last):
File "lineup.py", line 5, in <module>
writer.writerow(["name", "age", None])
~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^
csv.Error: cannot write None to CSV row