|
1 |
| -<p align="center"><img src="/images/logo.png#gh-light-mode-only" alt=""><img src="/images/logo-dark.png#gh-dark-mode-only" alt=""></p> |
| 1 | +<p align="center"> |
| 2 | + <picture> |
| 3 | + <source media="(prefers-color-scheme: dark)" srcset="/images/logo_dark_theme.svg"> |
| 4 | + <source media="(prefers-color-scheme: light)" srcset="/images/logo.svg"> |
| 5 | + <img alt="Shows a wtfpython logo." src="/images/logo.svg"> |
| 6 | + </picture> |
| 7 | +</p> |
2 | 8 | <h1 align="center">What the f*ck Python! 😱</h1>
|
3 | 9 | <p align="center">Exploring and understanding Python through surprising snippets.</p>
|
4 | 10 |
|
@@ -352,7 +358,14 @@ Makes sense, right?
|
352 | 358 | * All length 0 and length 1 strings are interned.
|
353 | 359 | * Strings are interned at compile time (`'wtf'` will be interned but `''.join(['w', 't', 'f'])` will not be interned)
|
354 | 360 | * Strings that are not composed of ASCII letters, digits or underscores, are not interned. This explains why `'wtf!'` was not interned due to `!`. CPython implementation of this rule can be found [here](https://github.com/python/cpython/blob/3.6/Objects/codeobject.c#L19)
|
355 |
| -  |
| 361 | +<p align="center"> |
| 362 | + <picture> |
| 363 | + <source media="(prefers-color-scheme: dark)" srcset="/images/string-intern/string_interning_dark_theme.svg"> |
| 364 | + <source media="(prefers-color-scheme: light)" srcset="/images/string-intern/string_interning.svg"> |
| 365 | + <img alt="Shows a string interning process." src="/images/string-intern/string_interning.svg"> |
| 366 | + </picture> |
| 367 | +</p> |
| 368 | +
|
356 | 369 | + When `a` and `b` are set to `"wtf!"` in the same line, the Python interpreter creates a new object, then references the second variable at the same time. If you do it on separate lines, it doesn't "know" that there's already `"wtf!"` as an object (because `"wtf!"` is not implicitly interned as per the facts mentioned above). It's a compile-time optimization. This optimization doesn't apply to 3.7.x versions of CPython (check this [issue](https://github.com/satwikkansal/wtfpython/issues/100) for more discussion).
|
357 | 370 | + A compile unit in an interactive environment like IPython consists of a single statement, whereas it consists of the entire module in case of modules. `a, b = "wtf!", "wtf!"` is single statement, whereas `a = "wtf!"; b = "wtf!"` are two statements in a single line. This explains why the identities are different in `a = "wtf!"; b = "wtf!"`, and also explain why they are same when invoked in `some_file.py`
|
358 | 371 | + The abrupt change in the output of the fourth snippet is due to a [peephole optimization](https://en.wikipedia.org/wiki/Peephole_optimization) technique known as Constant folding. This means the expression `'a'*20` is replaced by `'aaaaaaaaaaaaaaaaaaaa'` during compilation to save a few clock cycles during runtime. Constant folding only occurs for strings having a length of less than 21. (Why? Imagine the size of `.pyc` file generated as a result of the expression `'a'*10**10`). [Here's](https://github.com/python/cpython/blob/3.6/Python/peephole.c#L288) the implementation source for the same.
|
@@ -975,11 +988,23 @@ We didn't assign three `"X"`s, did we?
|
975 | 988 |
|
976 | 989 | When we initialize `row` variable, this visualization explains what happens in the memory
|
977 | 990 |
|
978 |
| - |
| 991 | +<p align="center"> |
| 992 | + <picture> |
| 993 | + <source media="(prefers-color-scheme: dark)" srcset="/images/tic-tac-toe/after_row_initialized_dark_theme.svg"> |
| 994 | + <source media="(prefers-color-scheme: light)" srcset="/images/tic-tac-toe/after_row_initialized.svg"> |
| 995 | + <img alt="Shows a memory segment after row is initialized." src="/images/tic-tac-toe/after_row_initialized.svg"> |
| 996 | + </picture> |
| 997 | +</p> |
979 | 998 |
|
980 | 999 | And when the `board` is initialized by multiplying the `row`, this is what happens inside the memory (each of the elements `board[0]`, `board[1]` and `board[2]` is a reference to the same list referred by `row`)
|
981 | 1000 |
|
982 |
| - |
| 1001 | +<p align="center"> |
| 1002 | + <picture> |
| 1003 | + <source media="(prefers-color-scheme: dark)" srcset="/images/tic-tac-toe/after_board_initialized_dark_theme.svg"> |
| 1004 | + <source media="(prefers-color-scheme: light)" srcset="/images/tic-tac-toe/after_board_initialized.svg"> |
| 1005 | + <img alt="Shows a memory segment after board is initialized." src="/images/tic-tac-toe/after_board_initialized.svg"> |
| 1006 | + </picture> |
| 1007 | +</p> |
983 | 1008 |
|
984 | 1009 | We can avoid this scenario here by not using `row` variable to generate `board`. (Asked in [this](https://github.com/satwikkansal/wtfpython/issues/68) issue).
|
985 | 1010 |
|
|
0 commit comments