explaingit

satwikkansal/wtfpython

36,941PythonAudience · developerComplexity · 1/5MaintainedLicenseSetup · easy

TLDR

A collection of surprising Python code snippets with detailed explanations of why they behave unexpectedly, covering language quirks and CPython implementation details.

Mindmap

mindmap
  root((repo))
    What it does
      Surprising code snippets
      Detailed explanations
      Edge case exploration
    Topics covered
      String interning
      Mutable defaults
      Operator behavior
      Scoping rules
    Organization
      Strain your brain
      Slippery slopes
      Hidden treasures
      Appearances deceptive
    Use cases
      Learning Python deeply
      Interview prep
      Understanding quirks
    Format
      Code examples
      Unexpected output
      Mechanism breakdown

Things people build with this

USE CASE 1

Deepen your understanding of Python's hidden behaviors and edge cases before a technical interview.

USE CASE 2

Learn why your Python code sometimes produces unexpected results by studying real examples with explanations.

USE CASE 3

Explore CPython implementation details like string interning and object identity to write more predictable code.

USE CASE 4

Teach others about Python's quirks and design decisions through interactive examples and detailed breakdowns.

Tech stack

Python

Getting it running

Difficulty · easy Time to first run · 5min
Use freely for any purpose, including commercial use, as long as you keep the copyright notice.

In plain English

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.

Copy-paste prompts

Prompt 1
I'm studying for a Python interview. Walk me through the examples in wtfpython about mutable default arguments and explain why they're dangerous.
Prompt 2
Show me how to use the wtfpython repository to understand the difference between 'is' and '==' in Python with concrete examples.
Prompt 3
Explain the string interning examples from wtfpython and why some identical strings are the same object in memory while others aren't.
Prompt 4
Help me understand the closure and scoping examples from wtfpython that show unexpected variable behavior in loops.
Prompt 5
Use wtfpython examples to explain floating-point precision issues in Python and when they'll bite you in real code.
Open on GitHub → Explain another repo

Generated 2026-05-18 · Model: sonnet-4-6 · Verify against the repo before relying on details.