satwikkansal /wtfpython

What the f*ck Python? 😱

★ 37,141⑂ 2,649👁 699⑃ 147● 227▤ 2◈ 4
PythonWTFPLCreated 2017-08-28
37,141
stars
+5+9+15
2,649
forks
699
watchers
147
prs
227
issues
2
releases
4
discussions

README

What the fck Python! 😱
Exploring and understanding Python through surprising snippets.

Translations: Chinese 中文 |
Vietnamese Tiếng Việt |
Spanish Español |
Korean 한국어 |
Russian Русский |
German Deutsch |
Persian فارسی |
Add translation

Other modes: Interactive Website | Interactive Notebook

Python, being a beautifully designed high-level and interpreter-based programming language,
provides us with many features for the programmer's comfort.
But sometimes, the outcomes of a Python snippet may not seem obvious at first sight.

Here's a fun project attempting to explain what exactly is happening under the hood for some counter-intuitive snippets
and lesser-known features in Python.

While some of the examples you see below may not be WTFs in the truest sense,
but they'll reveal some of the interesting parts of Python that you might be unaware of.
I find it a nice way to learn the internals of a programming language, and I believe that you'll find it interesting too!

If you're an experienced Python programmer, you can take it as a challenge to get most of them right in the first attempt
You may have already experienced some of them before, and I might be able to revive sweet old memories of yours! :sweat_smile:

PS: If you're a returning reader, you can learn about the new modifications here
(the examples marked with asterisk are the ones added in the latest major revision).

So, here we go...

 Table of Contents





- Structure of the Examples
  - ▶ Some fancy Title
- Usage
- 👀 Examples
  - Section: Strain your brain!
    - ▶ First things first! \
    - ▶ Strings can be tricky sometimes
    - ▶ Be careful with chained operations
    - ▶ How not to use is operator
    - ▶ Hash brownies
    - ▶ Deep down, we're all the same.
    - ▶ Disorder within order \
    - ▶ Keep trying... \
    - ▶ For what?
    - ▶ Evaluation time discrepancy
    - ▶ is not ... is not is (not ...)
    - ▶ A tic-tac-toe where X wins in the first attempt!
    - ▶ Schrödinger's variable
    - ▶ The chicken-egg problem \
    - ▶ Subclass relationships
    - ▶ Methods equality and identity
    - ▶ All-true-ation \
    - ▶ The surprising comma
    - ▶ Strings and the backslashes
    - ▶ not knot!
    - ▶ Half triple-quoted strings
    - ▶ What's wrong with booleans?
    - ▶ Class attributes and instance attributes
    - ▶ yielding None
    - ▶ Yielding from... return! \
    - ▶ Nan-reflexivity \
    - ▶ Mutating the immutable!
    - ▶ The disappearing variable from outer scope
    - ▶ The mysterious key type conversion
    - ▶ Let's see if you can guess this?
    - ▶ Exceeds the limit for integer string conversion
  - Section: Slippery Slopes
    - ▶ Modifying a dictionary while iterating over it
    - ▶ Stubborn del operation
    - ▶ The out of scope variable
    - ▶ Deleting a list item while iterating
    - ▶ Lossy zip of iterators \
    - ▶ Loop variables leaking out!
    - ▶ Beware of default mutable arguments!
    - ▶ Catching the Exceptions
    - ▶ Same operands, differe

Top issues

Discussions

Latest release

What the f*ck Python! (3.0) v3.0.0 2019-12-21
Excited to announce another major iteration of wtfpython after almost 2 years. Here's what changed since the last major update,

- Notebook based interactive version of wtfpython is now available.
- Added a lot of new examples (marked with an asterisk in the titles). Also, thanks to those who shared their gems.
- Revised some existing snippets for better understanding.
- Fix most of the open issues related to incomplete/incorrect/outdated explanation.
- Reorganized examples for a smoother reading flow.
- The wtfpython npm package (real wtf according to some readers) will no longer be ma

Snapshot: 2026-11-10T09:35:25Z · Data license Apache-2.0 · JSON