5 min readpythoncode reviewbest practices

5 Python Anti-Patterns Every Code Reviewer Catches

Mutable default arguments, late binding closures, bare excepts, and two more patterns that trip up Python developers daily.

Python is forgiving until it isn't. These five patterns are the ones that survive linters but get caught by every code reviewer β€” human or AI.

1. Mutable default arguments

def add_item(item, items=[]):  # πŸ’€
    items.append(item)
    return items

The default list is created once at definition time and shared across every call. Every caller after the first sees the previous caller's items. Use None:

def add_item(item, items=None):
    items = items if items is not None else []
    items.append(item)
    return items

2. Late binding in loops

callbacks = [lambda: print(i) for i in range(3)]
for c in callbacks:
    c()  # prints 2 2 2, not 0 1 2

Each lambda captures the variable, not the value. Bind it as a default arg:

callbacks = [lambda i=i: print(i) for i in range(3)]

3. Bare except clauses

try:
    risky_op()
except:           # catches KeyboardInterrupt, SystemExit, anything
    pass

Always specify the exception class. except Exception is barely better β€” name the actual error you expect.

4. Modifying a list while iterating

for item in items:
    if not item.is_valid:
        items.remove(item)  # skips items, breaks math

Build a new list with a comprehension instead: items = [i for i in items if i.is_valid].

5. Using == for None / True / False

if value == None:    # works, but slow + non-idiomatic
if value is None:    # βœ… identity check, idiomatic

Same for True and False β€” use is. == can be overridden by classes and produce surprising results.

How to catch these in your own code

Pylint and Ruff will catch some of these. The remaining ones β€” especially anti-patterns 1, 2, and 4 β€” are best caught by paste-into-an-AI-reviewer flow. Every one of these is in the training data; the AI flags them instantly. Try TrashMyCode on a Python file and watch how many it catches that your linter missed.


Want to apply this to your code?

Paste any code into TrashMyCode and get a brutally honest AI review in 30 seconds. Free, no credit card.

Roast my code

Keep reading