Early previewThis guide is in development. Items tagged verify are still being checked against official docs.
nebx Learn
GitHub
Menu
Home/Python/Anti-patterns
Py

Python

Readable, batteries-included, and the default language of AI.

Paradigm
Multi-paradigm, OOP, functional
Typing
Dynamic, strong, optional type hints
Memory
Reference counting + cycle GC
Runs
CPython bytecode interpreter
Concurrency
asyncio, threads (GIL), multiprocessing

Anti-patterns

Solutions that look reasonable but cause problems. First the Python-specific traps, then the classics that apply everywhere.

Python-specific

Anti-pattern
Do this instead
✕ Mutable default arguments (def f(x=[]))
✓ Use None and create the list inside: x = [] if x is None else x.
✕ Bare except:
✓ Catch specific exceptions; bare except also catches KeyboardInterrupt and SystemExit.
✕ from module import *
✓ Import names explicitly so readers and linters know where they come from.
✕ Membership tests on large lists in loops
✓ Use a set or dict for O(1) average lookups.
✕ Pure-Python loops over numeric arrays
✓ Vectorise with NumPy / pandas, which run the loop in C.

Universal anti-patterns

God object

One class knows and does too much. Split it by responsibility (SRP).

Spaghetti code

Tangled control flow with no structure. Refactor into small functions with clear inputs and outputs.

Golden hammer

Using one familiar tool or pattern for every problem. Pick the simplest fit.

Premature optimisation

Optimising before measuring. Profile first, then fix the real bottleneck.

Copy-paste programming

Duplicated logic drifts apart. Extract shared code once the duplication is real (rule of three).

Magic numbers and strings

Unexplained literals. Use named constants or enums.

Singleton abuse

Global state disguised as a pattern. Prefer dependency injection with a single-instance lifetime.

Lava flow

Dead or experimental code nobody dares to remove. Delete it; version control remembers.

Saved in this browser only. Part of From code to production (0/5).