Skip to content

Instantly share code, notes, and snippets.

@satwikkansal
Last active Jul 28, 2020
Embed
What would you like to do?
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<p align=\"center\"><img src=\"images/logo.png\" alt=\"\"></p>\n",
"<h1 align=\"center\">What the f*ck Python! 🐍</h1>\n",
"<p align=\"center\">An interesting collection of surprising snippets and lesser-known Python features.</p>\n",
"\n",
"Translations: [Chinese 中文](https://github.com/leisurelicht/wtfpython-cn)\n",
"\n",
"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.\n",
"\n",
"Here is a fun project attempting to explain what exactly is happening under the hood for some counter-intuitive snippets and lesser-known features in Python.\n",
"\n",
"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 beliereve that you'll find it interesting too!\n",
"\n",
"If you're an experienced Python programmer, you can take it as a challenge to get most of them right in 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:\n",
"\n",
"PS: If you're a returning reader, you can learn about the new modifications [here](https://github.com/satwikkansal/wtfpython/releases/).\n",
"\n",
"So, here we go...\n",
"\n",
"# Table of Contents\n",
"\n",
"<!-- Generated using \"markdown-toc -i README.md --maxdepth 3\"-->\n",
"<!-- toc -->\n",
"\n",
"- [Structure of the Examples](#structure-of-the-examples)\n",
" + [▶ Some fancy Title](#-some-fancy-title)\n",
"- [Usage](#usage)\n",
"- [👀 Examples](#%F0%9F%91%80-examples)\n",
" * [Section: Strain your brain!](#section-strain-your-brain)\n",
" + [▶ First things first!](#-first-things-first)\n",
" + [▶ Strings can be tricky sometimes](#-strings-can-be-tricky-sometimes)\n",
" + [▶ Splitsies](#-splitsies)\n",
" + [▶ Hash brownies](#-hash-brownies)\n",
" + [▶ Disorder within order](#-disorder-within-order)\n",
" + [▶ Keep trying...](#-keep-trying)\n",
" + [▶ Deep down, we're all the same.](#-deep-down-were-all-the-same)\n",
" + [▶ For what?](#-for-what)\n",
" + [▶ Evaluation time discrepancy](#-evaluation-time-discrepancy)\n",
" + [▶ How not to use `is` operator](#-how-not-to-use-is-operator)\n",
" + [▶ A tic-tac-toe where X wins in the first attempt!](#-a-tic-tac-toe-where-x-wins-in-the-first-attempt)\n",
" + [▶ The sticky output function](#-the-sticky-output-function)\n",
" + [▶ The chicken-egg problem](#-the-chicken-egg-problem)\n",
" + [▶ `is not ...` is not `is (not ...)`](#-is-not--is-not-is-not-)\n",
" + [▶ The surprising comma](#-the-surprising-comma)\n",
" + [▶ Strings and the backslashes](#-strings-and-the-backslashes)\n",
" + [▶ not knot!](#-not-knot)\n",
" + [▶ Half triple-quoted strings](#-half-triple-quoted-strings)\n",
" + [▶ Midnight time doesn't exist?](#-midnight-time-doesnt-exist)\n",
" + [▶ What's wrong with booleans?](#-whats-wrong-with-booleans)\n",
" + [▶ Class attributes and instance attributes](#-class-attributes-and-instance-attributes)\n",
" + [▶ Non-reflexive class method](#-non-reflexive-class-method)\n",
" + [▶ yielding None](#-yielding-none)\n",
" + [▶ Nan-reflexivity](#-nan-reflexivity)\n",
" + [▶ Mutating the immutable!](#-mutating-the-immutable)\n",
" + [▶ The disappearing variable from outer scope](#-the-disappearing-variable-from-outer-scope)\n",
" + [▶ Yielding from... return!](#-yielding-from-return)\n",
" + [▶ Lossy zip of iterators](#-lossy-zip-of-iterators)\n",
" + [▶ Subclass relationships](#-subclass-relationships)\n",
" + [▶ The mysterious key type conversion](#-the-mysterious-key-type-conversion)\n",
" + [▶ Let's see if you can guess this?](#-lets-see-if-you-can-guess-this)\n",
" * [Section: Appearances are deceptive!](#section-appearances-are-deceptive)\n",
" + [▶ Skipping lines?](#-skipping-lines)\n",
" + [▶ Teleportation](#-teleportation)\n",
" + [▶ Well, something is fishy...](#-well-something-is-fishy)\n",
" * [Section: Slippery Slopes](#section-slippery-slopes)\n",
" + [▶ Modifying a dictionary while iterating over it](#-modifying-a-dictionary-while-iterating-over-it)\n",
" + [▶ Stubborn `del` operation](#-stubborn-del-operation)\n",
" + [▶ Deleting a list item while iterating](#-deleting-a-list-item-while-iterating)\n",
" + [▶ Loop variables leaking out!](#-loop-variables-leaking-out)\n",
" + [▶ Beware of default mutable arguments!](#-beware-of-default-mutable-arguments)\n",
" + [▶ Catching the Exceptions](#-catching-the-exceptions)\n",
" + [▶ Same operands, different story!](#-same-operands-different-story)\n",
" + [▶ The out of scope variable](#-the-out-of-scope-variable)\n",
" + [▶ Be careful with chained operations](#-be-careful-with-chained-operations)\n",
" + [▶ Name resolution ignoring class scope](#-name-resolution-ignoring-class-scope)\n",
" + [▶ Needles in a Haystack](#-needles-in-a-haystack)\n",
" + [▶ Wild imports](#-wild-imports)\n",
" * [Section: Read the docs](#section-read-the-docs)\n",
" + [▶ All sorted?](#-all-sorted)\n",
" + [▶ All-true-ation](#-all-true-ation)\n",
" * [Section: The Hidden treasures!](#section-the-hidden-treasures)\n",
" + [▶ Okay Python, Can you make me fly?](#-okay-python-can-you-make-me-fly)\n",
" + [▶ `goto`, but why?](#-goto-but-why)\n",
" + [▶ Brace yourself!](#-brace-yourself)\n",
" + [▶ Let's meet Friendly Language Uncle For Life](#-lets-meet-friendly-language-uncle-for-life)\n",
" + [▶ Even Python understands that love is complicated](#-even-python-understands-that-love-is-complicated)\n",
" + [▶ Yes, it exists!](#-yes-it-exists)\n",
" + [▶ Ellipsis](#-ellipsis)\n",
" + [▶ Inpinity](#-inpinity)\n",
" + [▶ Let's mangle](#-lets-mangle)\n",
" * [Section: Miscellaneous](#section-miscellaneous)\n",
" + [▶ `+=` is faster](#--is-faster)\n",
" + [▶ Let's make a giant string!](#-lets-make-a-giant-string)\n",
" + [▶ Minor Ones](#-minor-ones)\n",
"- [Contributing](#contributing)\n",
"- [Acknowledgements](#acknowledgements)\n",
"- [🎓 License](#%F0%9F%8E%93-license)\n",
" * [Surprise your friends too?](#surprise-your-friends-too)\n",
" * [Need a pdf version?](#need-a-pdf-version)\n",
"\n",
"<!-- tocstop -->\n",
"\n",
"# Structure of the Examples\n",
"\n",
"All the examples are structured like below:\n",
"\n",
"> ### ▶ Some fancy Title\n",
">\n",
"> ```py\n",
"> # Set up the code.\n",
"> # Preparation for the magic...\n",
"> ```\n",
">\n",
"> **Output (Python version(s)):**\n",
">\n",
"> ```py\n",
"> >>> triggering_statement\n",
"> Some unexpected output\n",
"> ```\n",
"> (Optional): One line describing the unexpected output.\n",
">\n",
">\n",
"> #### 💡 Explanation:\n",
">\n",
"> * Brief explanation of what's happening and why is it happening.\n",
"> ```py\n",
"> # Set up code\n",
"> # More examples for further clarification (if necessary)\n",
"> ```\n",
"> **Output (Python version(s)):**\n",
">\n",
"> ```py\n",
"> >>> trigger # some example that makes it easy to unveil the magic\n",
"> # some justified output\n",
"> ```\n",
"\n",
"**Note:** All the examples are tested on Python 3.5.2 interactive interpreter, and they should work for all the Python versions unless explicitly specified before the output.\n",
"\n",
"# Usage\n",
"\n",
"A nice way to get the most out of these examples, in my opinion, will be to just read them chronologically, and for every example:\n",
"- Carefully read the initial code for setting up the example. If you're an experienced Python programmer, most of the times you will successfully anticipate what's going to happen next.\n",
"- Read the output snippets and,\n",
" + Check if the outputs are the same as you'd expect.\n",
" + Make sure if you know the exact reason behind the output being the way it is.\n",
" - If the answer is no (which is perfectly okay), take a deep breath, and read the explanation (and if you still don't understand, shout out! and create an issue [here](https://github.com/satwikkansal/wtfPython)).\n",
" - If yes, give a gentle pat on your back, and you may skip to the next example.\n",
"\n",
"PS: You can also read WTFPython at the command line. There's a pypi package and an npm package (which supports colored formatting) for the same.\n",
"\n",
"To install the npm package [`wtfpython`](https://www.npmjs.com/package/wtfpython)\n",
"```sh\n",
"$ npm install -g wtfpython\n",
"```\n",
"\n",
"Alternatively, to install the pypi package [`wtfpython`](https://pypi.python.org/pypi/wtfpython)\n",
"```sh\n",
"$ pip install wtfpython -U\n",
"```\n",
"\n",
"Now, just run `wtfpython` at the command line which will open this collection in your selected `$PAGER`.\n",
"\n",
"---\n",
"\n",
"# 👀 Examples\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### ▶ First things first!\n",
"\n",
"<!-- Example ID: d3d73936-3cf1-4632-b5ab-817981338863 -->\n",
"\n",
"For some reasons, \"Walrus\" operator (`:=`) has become a very popular feature in the language. Let's check it out,\n",
"\n",
"1\\.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
"'wtf_walrus'\n"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = \"wtf_walrus\"\n",
"a\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
"File \"<stdin>\", line 1\n",
" a := \"wtf_walrus\"\n",
" ^\n",
"SyntaxError: invalid syntax\n",
"\n"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a := \"wtf_walrus\"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
"'wtf_walrus'\n"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(a := \"wtf_walrus\") # This works though\n",
"a\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"2 \\.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
"(6, 9)\n",
"\n"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = 6, 9\n",
"a\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
"6\n",
"\n"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(a := 6, 9)\n",
"a\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
"(6, 9)\n"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a, b = 6, 9 # Typcial unpacking\n",
"a, b\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
" File \"<stdin>\", line 1\n",
" (a, b = 6, 9)\n",
" ^\n",
"SyntaxError: invalid syntax\n",
"\n"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(a, b = 16, 19) # Oops\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
"(6, 16, 19)\n",
"\n"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(a, b := 16, 19) # This prints out a weird 3-tuple\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
"6\n",
"\n"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a # a is still unchanged?\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
"16\n"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"b\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"\n",
"\n",
"💡 Explanation\n",
"\n",
"**Quick walrus operator refresher**\n",
"\n",
"The Walrus operator (`:=`) was introduced in Python 3.8, it can be useful in situatitions where you'd want to assing values to variables within an expression.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": []
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def some_func():\n",
"\t\t# Assume some expensive computation here\n",
"\t\t# time.sleep(1000)\n",
"\t\treturn 5\n",
"\n",
"# So instead of,\n",
"if some_func():\n",
"\t\tprint(some_func()) # Which is bad practice since computation is happening twice\n",
"\n",
"# or\n",
"a = some_func()\n",
"if a:\n",
"\tprint(a)\n",
"\n",
"# Now you can concisely write\n",
"if a := some_func():\n",
"\t\tprint(a)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"**Output (> 3.8):**\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": []
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"5\n",
"5\n",
"5\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"This helped save one line of code, and implicitly prevented invoking `some_func` twice.\n",
"\n",
"- Unparenthesized \"assignment expression\" (use of walrus operator), is restricted at top level, hence the `SyntaxError` in the `a := \"wtf_walrus\"` statement of first snippet. Parenthesizing it worked as expected and assigned `a`. \n",
"\n",
"- As usual, parenthesizing of expression containing `=` operator is not allowed. Hence the syntax error in `(a, b = 6, 9)`. \n",
"\n",
"- The syntax of the Walrus operator is of the form `NAME: expr`, where `NAME` is a valid identifier and `expr` is a valid expression. Hence, iterable packing and unpacking are not supported which means, \n",
"\n",
" - `(a := 6, 9)` is equivalent to `((a := 6), 9)` and ultimately `(a, 9) ` (where `a`'s value is 6')\n",
"\n",
" ```py\n",
" >>> (a := 6, 9) == ((a := 6), 9)\n",
" True\n",
" >>> x = (a := 696, 9)\n",
" >>> x\n",
" (696, 9)\n",
" >>> x[0] is a # Both reference same memory location\n",
" True\n",
" ```\n",
"\n",
" - Similarly, `(a, b := 16, 19)` is equivalent to `(a, (b := 16), 19)` which is nothing but a 3-tuple. \n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### ▶ Strings can be tricky sometimes\n",
"\n",
"<!-- Example ID: 30f1d3fc-e267-4b30-84ef-4d9e7091ac1a --->\n",
"1\\.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
"140420665652016\n"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = \"some_string\"\n",
"id(a)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
"140420665652016\n"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"id(\"some\" + \"_\" + \"string\") # Notice that both the ids are same.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"2\\.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
"True\n",
"\n"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = \"wtf\"\n",
"b = \"wtf\"\n",
"a is b\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
"False\n",
"\n"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = \"wtf!\"\n",
"b = \"wtf!\"\n",
"a is b\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"3\\.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
"True\n",
"\n"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a, b = \"wtf!\", \"wtf!\"\n",
"a is b # All versions except 3.7.x\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
"False\n"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = \"wtf!\"; b = \"wtf!\"\n",
"a is b # This will print True or False depending on where you're invoking it (python shell / ipython / as a script)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": []
}
],
"source": [
"# This time in file some_file.py\n",
"a = \"wtf!\"\n",
"b = \"wtf!\"\n",
"print(a is b)\n",
"\n",
"# prints True when the module is invoked!\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"4\\.\n",
"\n",
"**Output (< Python3.7 )**\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
"True\n"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"'a' * 20 is 'aaaaaaaaaaaaaaaaaaaa'\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
"False\n"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"'a' * 21 is 'aaaaaaaaaaaaaaaaaaaaa'\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"Makes sense, right?\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 💡 Explanation:\n",
"+ The behavior in first and second snippets is due to a CPython optimization (called string interning) that tries to use existing immutable objects in some cases rather than creating a new object every time.\n",
"+ After being \"interned\", many variables may reference the same string object in memory (saving memory thereby).\n",
"+ In the snippets above, strings are implicitly interned. The decision of when to implicitly intern a string is implementation dependent. There are some rules that can be used to guess if a string will be interned or not:\n",
" * All length 0 and length 1 strings are interned.\n",
" * Strings are interned at compile time (`'wtf'` will be interned but `''.join(['w', 't', 'f']` will not be interned)\n",
" * 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)\n",
" <img src=\"images/string-intern/string_intern.png\" alt=\"\">\n",
"+ 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 an 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).\n",
"+ The compile unit in interactive environment like ipython consists of 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 2 statements in 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`\n",
"+ The abrupt change in 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 length less than 20. (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.\n",
"+ Note: In Python 3.7, Constant folding was moved out from peephole optimizer to the new AST optimizer with some change in logic as well, so the third snippet doesn't work for Python 3.7. You can read more about the change [here](https://bugs.python.org/issue11549). \n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### ▶ Hash brownies\n",
"<!-- Example ID: eb17db53-49fd-4b61-85d6-345c5ca213ff --->\n",
"1\\.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": []
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"some_dict = {}\n",
"some_dict[5.5] = \"JavaScript\"\n",
"some_dict[5.0] = \"Ruby\"\n",
"some_dict[5] = \"Python\"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"**Output:**\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
"\"JavaScript\"\n"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"some_dict[5.5]\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
"\"Python\"\n"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"some_dict[5.0] # \"Python\" destroyed the existence of \"Ruby\"?\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
"\"Python\"\n",
"\n"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"some_dict[5] \n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
"complex\n"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"complex_five = 5 + 0j\n",
"type(complex_five)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
"\"Python\"\n"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"some_dict[complex_five]\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"So, why is Python all over the place?\n",
"\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 💡 Explanation\n",
"\n",
"* Python dictionaries check for equality and compare the hash value to determine if two keys are the same.\n",
"* Immutable objects with same value always have the same hash in Python.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
" True\n"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
" 5 == 5.0 == 5 + 0j\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
" True\n"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
" hash(5) == hash(5.0) == hash(5 + 0j)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" **Note:** Objects with different values may also have same hash (known as [hash collision](https://en.wikipedia.org/wiki/Collision_(computer_science))).\n",
"* When the statement `some_dict[5] = \"Python\"` is executed, the existing value \"Ruby\" is overwritten with \"Python\" because Python recognizes `5` and `5.0` as the same keys of the dictionary `some_dict`.\n",
"* This StackOverflow [answer](https://stackoverflow.com/a/32211042/4354153) explains the rationale behind it.\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### ▶ Deep down, we're all the same.\n",
"<!-- Example ID: 8f99a35f-1736-43e2-920d-3b78ec35da9b --->\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": []
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"class WTF:\n",
" pass\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"**Output:**\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
"False\n"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"WTF() == WTF() # two different instances can't be equal\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
"False\n"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"WTF() is WTF() # identities are also different\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
"True\n"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"hash(WTF()) == hash(WTF()) # hashes _should_ be different as well\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
"True\n"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"id(WTF()) == id(WTF())\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 💡 Explanation:\n",
"\n",
"* When `id` was called, Python created a `WTF` class object and passed it to the `id` function. The `id` function takes its `id` (its memory location), and throws away the object. The object is destroyed.\n",
"* When we do this twice in succession, Python allocates the same memory location to this second object as well. Since (in CPython) `id` uses the memory location as the object id, the id of the two objects is the same.\n",
"* So, object's id is unique only for the lifetime of the object. After the object is destroyed, or before it is created, something else can have the same id.\n",
"* But why did the `is` operator evaluated to `False`? Let's see with this snippet.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": []
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
" class WTF(object):\n",
" def __init__(self): print(\"I\")\n",
" def __del__(self): print(\"D\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
" **Output:**\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
" I\n",
" I\n",
" D\n",
" D\n",
" False\n"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
" WTF() is WTF()\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
" I\n",
" D\n",
" I\n",
" D\n",
" True\n"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
" id(WTF()) == id(WTF())\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" As you may observe, the order in which the objects are destroyed is what made all the difference here.\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### ▶ Disorder within order\n",
"<!-- Example ID: 91bff1f8-541d-455a-9de4-6cd8ff00ea66 --->\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": []
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from collections import OrderedDict\n",
"\n",
"dictionary = dict()\n",
"dictionary[1] = 'a'; dictionary[2] = 'b';\n",
"\n",
"ordered_dict = OrderedDict()\n",
"ordered_dict[1] = 'a'; ordered_dict[2] = 'b';\n",
"\n",
"another_ordered_dict = OrderedDict()\n",
"another_ordered_dict[2] = 'b'; another_ordered_dict[1] = 'a';\n",
"\n",
"class DictWithHash(dict):\n",
" \"\"\"\n",
" A dict that also implements __hash__ magic.\n",
" \"\"\"\n",
" __hash__ = lambda self: 0\n",
"\n",
"class OrderedDictWithHash(OrderedDict):\n",
" \"\"\"\n",
" An OrderedDict that also implements __hash__ magic.\n",
" \"\"\"\n",
" __hash__ = lambda self: 0\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"**Output**\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
"True\n"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dictionary == ordered_dict # If a == b\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
"True\n"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dictionary == another_ordered_dict # and b == c\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
"False\n",
"\n",
"# We all know that a set consists of only unique elements,\n",
"# let's try making a set of these dictionaries and see what happens...\n",
"\n"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ordered_dict == another_ordered_dict # the why isn't c == a ??\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
"Traceback (most recent call last):\n",
" File \"<stdin>\", line 1, in <module>\n",
"TypeError: unhashable type: 'dict'\n",
"\n",
"# Makes sense since dict don't have __hash__ implemented, let's use\n",
"# our wrapper classes.\n"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len({dictionary, ordered_dict, another_ordered_dict})\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
"1\n"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dictionary = DictWithHash()\n",
"dictionary[1] = 'a'; dictionary[2] = 'b';\n",
"ordered_dict = OrderedDictWithHash()\n",
"ordered_dict[1] = 'a'; ordered_dict[2] = 'b';\n",
"another_ordered_dict = OrderedDictWithHash()\n",
"another_ordered_dict[2] = 'b'; another_ordered_dict[1] = 'a';\n",
"len({dictionary, ordered_dict, another_ordered_dict})\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
"2\n"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len({ordered_dict, another_ordered_dict, dictionary}) # changing the order\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"What is going on here?\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 💡 Explanation:\n",
"\n",
"- The reason why intransitive equality didn't hold among `dictionary`, `ordered_dict` and `another_ordered_dict` is because of the way `__eq__` method is implemented in `OrderedDict` class. From the [docs](https://docs.python.org/3/library/collections.html#ordereddict-objects)\n",
" \n",
" > Equality tests between OrderedDict objects are order-sensitive and are implemented as `list(od1.items())==list(od2.items())`. Equality tests between `OrderedDict` objects and other Mapping objects are order-insensitive like regular dictionaries.\n",
"- The reason for this equality is behavior is that it allows `OrderedDict` objects to be directly substituted anywhere a regular dictionary is used.\n",
"- Okay, so why did changing the order affect the lenght of the generated `set` object? The answer is the lack of intransitive equality only. Since sets are \"unordered\" collections of unique elements, the order in which elements are inserted shouldn't matter. But in this case, it does matter. Let's break it down a bit,\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
" True\n"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
" some_set = set()\n",
" some_set.add(dictionary) # these are the mapping objects from the snippets above\n",
" ordered_dict in some_set\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
" 1\n"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
" some_set.add(ordered_dict)\n",
" len(some_set)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
" True\n"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
" another_ordered_dict in some_set\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
" 1\n",
"\n"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
" some_set.add(another_ordered_dict)\n",
" len(some_set)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
" False\n"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
" another_set = set()\n",
" another_set.add(ordered_dict)\n",
" another_ordered_dict in another_set\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
" 2\n"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
" another_set.add(another_ordered_dict)\n",
" len(another_set)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
" True\n"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
" dictionary in another_set\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
" 2\n"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
" another_set.add(another_ordered_dict)\n",
" len(another_set)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" So the inconsistency is due to `another_ordered_dict in another_set` being `False` because `ordered_dict` was already present in `another_set` and as observed before, `ordered_dict == another_ordered_dict` is `False`.\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### ▶ Keep trying...\n",
"<!-- Example ID: b4349443-e89f-4d25-a109-82616be9d41a --->\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": []
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def some_func():\n",
" try:\n",
" return 'from_try'\n",
" finally:\n",
" return 'from_finally'\n",
"\n",
"def another_func(): \n",
" for _ in range(3):\n",
" try:\n",
" continue\n",
" finally:\n",
" print(\"Finally!\")\n",
"\n",
"def one_more_func(): # A gotcha!\n",
" try:\n",
" for i in range(3):\n",
" try:\n",
" 1 / i\n",
" except ZeroDivisionError:\n",
" # Let's throw it here and handle it outside for loop\n",
" raise ZeroDivisionError(\"A trivial divide by zero error\")\n",
" finally:\n",
" print(\"Iteration\", i)\n",
" break\n",
" except ZeroDivisionError as e:\n",
" print(\"Zero division error ocurred\", e)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"**Output:**\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
"'from_finally'\n",
"\n"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"some_func()\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
"Finally!\n",
"Finally!\n",
"Finally!\n",
"\n"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"another_func()\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
"Traceback (most recent call last):\n",
" File \"<stdin>\", line 1, in <module>\n",
"ZeroDivisionError: division by zero\n",
"\n"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"1 / 0\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
"Iteration 0\n",
"\n"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"one_more_func()\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 💡 Explanation:\n",
"\n",
"- When a `return`, `break` or `continue` statement is executed in the `try` suite of a \"try…finally\" statement, the `finally` clause is also executed on the way out.\n",
"- The return value of a function is determined by the last `return` statement executed. Since the `finally` clause always executes, a `return` statement executed in the `finally` clause will always be the last one executed.\n",
"- The caveat here is, if the finally clause executes a `return` or `break` statement, the temporarily saved exception is discarded.\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### ▶ For what?\n",
"<!-- Example ID: 64a9dccf-5083-4bc9-98aa-8aeecde4f210 --->\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": []
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"some_string = \"wtf\"\n",
"some_dict = {}\n",
"for i, some_dict[i] in enumerate(some_string):\n",
" i = 10\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"**Output:**\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
"{0: 'w', 1: 't', 2: 'f'}\n"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"some_dict # An indexed dict appears.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 💡 Explanation:\n",
"\n",
"* A `for` statement is defined in the [Python grammar](https://docs.python.org/3/reference/grammar.html) as:\n",
" ```\n",
" for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite]\n",
" ```\n",
" Where `exprlist` is the assignment target. This means that the equivalent of `{exprlist} = {next_value}` is **executed for each item** in the iterable.\n",
" An interesting example that illustrates this:\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": []
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
" for i in range(4):\n",
" print(i)\n",
" i = 10\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
" **Output:**\n",
" ```\n",
" 0\n",
" 1\n",
" 2\n",
" 3\n",
" ```\n",
"\n",
" Did you expect the loop to run just once?\n",
"\n",
" **💡 Explanation:**\n",
"\n",
" - The assignment statement `i = 10` never affects the iterations of the loop because of the way for loops work in Python. Before the beginning of every iteration, the next item provided by the iterator (`range(4)` this case) is unpacked and assigned the target list variables (`i` in this case).\n",
"\n",
"* The `enumerate(some_string)` function yields a new value `i` (a counter going up) and a character from the `some_string` in each iteration. It then sets the (just assigned) `i` key of the dictionary `some_dict` to that character. The unrolling of the loop can be simplified as:\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": []
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
" i, some_dict[i] = (0, 'w')\n",
" i, some_dict[i] = (1, 't')\n",
" i, some_dict[i] = (2, 'f')\n",
" some_dict\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### ▶ Evaluation time discrepancy\n",
"<!-- Example ID: 6aa11a4b-4cf1-467a-b43a-810731517e98 --->\n",
"1\\.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": []
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"array = [1, 8, 15]\n",
"# A typical generator expresion\n",
"gen = (x for x in array if array.count(x) > 0)\n",
"array = [2, 8, 22]\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"**Output:**\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[8]\n"
]
}
],
"source": [
"print(list(gen)) # Where did the other values go?\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"2\\.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": []
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"array_1 = [1,2,3,4]\n",
"gen_1 = (x for x in array_1)\n",
"array_1 = [1,2,3,4,5]\n",
"\n",
"array_2 = [1,2,3,4]\n",
"gen_2 = (x for x in array_2)\n",
"array_2[:] = [1,2,3,4,5]\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"**Output:**\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 2, 3, 4]\n",
"\n"
]
}
],
"source": [
"print(list(gen_1))\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 2, 3, 4, 5]\n"
]
}
],
"source": [
"print(list(gen_2))\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"3\\.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": []
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"array_3 = [1, 2, 3]\n",
"array_4 = [10, 20, 30]\n",
"gen = (i + j for i in array_3 for j in array_4)\n",
"\n",
"array_3 = [4, 5, 6]\n",
"array_4 = [400, 500, 600]\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"**Output:**\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[401, 501, 601, 402, 502, 602, 403, 503, 603]\n"
]
}
],
"source": [
"print(list(gen))\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 💡 Explanation\n",
"\n",
"- In a [generator](https://wiki.python.org/moin/Generators) expression, the `in` clause is evaluated at declaration time, but the conditional clause is evaluated at runtime.\n",
"- So before runtime, `array` is re-assigned to the list `[2, 8, 22]`, and since out of `1`, `8` and `15`, only the count of `8` is greater than `0`, the generator only yields `8`.\n",
"- The differences in the output of `g1` and `g2` in the second part is due the way variables `array_1` and `array_2` are re-assigned values.\n",
"- In the first case, `array_1` is binded to the new object `[1,2,3,4,5]` and since the `in` clause is evaluated at the declaration time it still refers to the old object `[1,2,3,4]` (which is not destroyed).\n",
"- In the second case, the slice assignment to `array_2` updates the same old object `[1,2,3,4]` to `[1,2,3,4,5]`. Hence both the `g2` and `array_2` still have reference to the same object (which has now been updated to `[1,2,3,4,5]`).\n",
"- Okay, going by the logic discussed so far, shouldn't be the value of `list(g)` in the third snippet be `[11, 21, 31, 12, 22, 32, 13, 23, 33]`? (because `array_3` and `array_4` are going to behave just like `array_1`). The reason why (only) `array_4` values got updated is explained in [PEP-289](https://www.python.org/dev/peps/pep-0289/#the-details)\n",
" \n",
" > Only the outermost for-expression is evaluated immediately, the other expressions are deferred until the generator is run.\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### ▶ How not to use `is` operator\n",
"<!-- Example ID: 230fa2ac-ab36-4ad1-b675-5f5a1c1a6217 --->\n",
"The following is a very famous example present all over the internet.\n",
"\n",
"1\\.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
"True\n",
"\n"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = 256\n",
"b = 256\n",
"a is b\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
"False\n"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = 257\n",
"b = 257\n",
"a is b\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"2\\.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
"False\n",
"\n"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = []\n",
"b = []\n",
"a is b\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
"True\n"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = tuple()\n",
"b = tuple()\n",
"a is b\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"3\\.\n",
"**Output**\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
"True\n"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a, b = 257, 257\n",
"a is b\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"**Output (Python 3.7.x specifically)**\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
">> a is b\n",
"False\n"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a, b = 257, 257\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 💡 Explanation:\n",
"\n",
"**The difference between `is` and `==`**\n",
"\n",
"* `is` operator checks if both the operands refer to the same object (i.e., it checks if the identity of the operands matches or not).\n",
"* `==` operator compares the values of both the operands and checks if they are the same.\n",
"* So `is` is for reference equality and `==` is for value equality. An example to clear things up,\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
" False\n"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
" class A: pass\n",
" A() is A() # These are two empty objects at two different memory locations.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"**`256` is an existing object but `257` isn't**\n",
"\n",
"When you start up python the numbers from `-5` to `256` will be allocated. These numbers are used a lot, so it makes sense just to have them ready.\n",
"\n",
"Quoting from https://docs.python.org/3/c-api/long.html\n",
"> The current implementation keeps an array of integer objects for all integers between -5 and 256, when you create an int in that range you just get back a reference to the existing object. So it should be possible to change the value of 1. I suspect the behavior of Python, in this case, is undefined. :-)\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
"10922528\n"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"id(256)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
"10922528\n"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = 256\n",
"b = 256\n",
"id(a)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
"10922528\n"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"id(b)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
"140084850247312\n"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"id(257)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"140084850247440\n"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = 257\n",
"y = 257\n",
"id(x)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
"140084850247344\n"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"id(y)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"Here the interpreter isn't smart enough while executing `y = 257` to recognize that we've already created an integer of the value `257,` and so it goes on to create another object in the memory.\n",
"\n",
"Similar optimization applies to other **immutable** objects like empty tuples as well. Since lists are mutable, that's why `[] is []` will return `False` and `() is ()` will return `True`. This explains our second snippet. Let's move on to the third one, \n",
"\n",
"**Both `a` and `b` refer to the same object when initialized with same value in the same line.**\n",
"\n",
"**Output**\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
"140640774013296\n"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a, b = 257, 257\n",
"id(a)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
"140640774013296\n"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"id(b)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
"140640774013392\n"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = 257\n",
"b = 257\n",
"id(a)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
"140640774013488\n"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"id(b)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"* When a and b are set to `257` 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 `257` as an object.\n",
"\n",
"* It's a compiler optimization and specifically applies to the interactive environment. When you enter two lines in a live interpreter, they're compiled separately, therefore optimized separately. If you were to try this example in a `.py` file, you would not see the same behavior, because the file is compiled all at once. This optimization is not limited to integers, it works for other immutable data types like strings (check the \"Strings are tricky example\") and floats as well,\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
" True\n"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
" a, b = 257.0, 257.0\n",
" a is b\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"* Why didn't this work for Python 3.7? The abstract reason is because such compiler optimizations are implementation specific (i.e. may change with version, OS, etc). I'm still figuring out what exact implementation change cause the issue, you can check out this [issue](https://github.com/satwikkansal/wtfpython/issues/100) for updates.\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### ▶ `is not ...` is not `is (not ...)`\n",
"<!-- Example ID: b26fb1ed-0c7d-4b9c-8c6d-94a58a055c0d --->\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
"True\n"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"'something' is not None\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
"False\n"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"'something' is (not None)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 💡 Explanation\n",
"\n",
"- `is not` is a single binary operator, and has behavior different than using `is` and `not` separated.\n",
"- `is not` evaluates to `False` if the variables on either side of the operator point to the same object and `True` otherwise.\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### ▶ A tic-tac-toe where X wins in the first attempt!\n",
"<!-- Example ID: 69329249-bdcb-424f-bd09-cca2e6705a7a --->\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": []
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Let's initialize a row\n",
"row = [\"\"] * 3 #row i['', '