Deepen your understanding of Python's hidden behaviors and edge cases before a technical interview.
Learn why your Python code sometimes produces unexpected results by studying real examples with explanations.
Explore CPython implementation details like string interning and object identity to write more predictable code.
Teach others about Python's quirks and design decisions through interactive examples and detailed breakdowns.
wtfpython is a collection of surprising, counter-intuitive, or confusing Python code snippets, each followed by a detailed explanation of what is actually happening. The project's tagline is "What the f*ck Python", capturing the reaction developers often have when they run a piece of valid Python code and get output that seems completely wrong. The goal is to explore and explain the underlying mechanics that cause these surprising behaviors. Each example follows a consistent structure: a short code snippet is shown, the output is displayed (which is unexpected), and then a detailed explanation digs into the Python language rules, CPython implementation details, or subtle design decisions that produce that output. Topics covered include Python's string interning (when two strings that look identical are or are not the same object in memory), the behavior of mutable default function arguments, how the "is" operator differs from equality checks, scoping rules in closures, how floating-point numbers behave, the timing of variable evaluation in loops, and many other nuanced behaviors. The examples are organized into sections: "Strain your brain" (the truly surprising ones), "Slippery Slopes" (common pitfalls that lead to real bugs), "Hidden treasures" (obscure but valid Python features), and "Appearances are deceptive" (code that looks different than it behaves). An interactive version is available as a Google Colab notebook. You would use this repository if you are learning Python and want to understand edge cases more deeply, if you are preparing for interviews that probe Python knowledge, or if you simply enjoy learning a language through its quirks. It is a documentation and educational resource, not runnable application code, and the language throughout is Python.
Generated 2026-05-18 · Model: sonnet-4-6 · Verify against the repo before relying on details.