From ff679bcaf1dbfb80cc0443f9ae39e49781006da9 Mon Sep 17 00:00:00 2001 From: BrandonThaiTran Date: Wed, 29 Jul 2020 12:37:53 -0400 Subject: [PATCH] fix grammatical errors and confusing wording --- tutorials/scope_resolution_legb_rule.ipynb | 346 +++++++++------------ 1 file changed, 155 insertions(+), 191 deletions(-) diff --git a/tutorials/scope_resolution_legb_rule.ipynb b/tutorials/scope_resolution_legb_rule.ipynb index 58adb8a..01e4c95 100644 --- a/tutorials/scope_resolution_legb_rule.ipynb +++ b/tutorials/scope_resolution_legb_rule.ipynb @@ -12,9 +12,12 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "metadata": { - "collapsed": false + "collapsed": false, + "jupyter": { + "outputs_hidden": false + } }, "outputs": [], "source": [ @@ -23,22 +26,14 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Sebastian Raschka 01/27/2016 \n", - "\n", - "CPython 3.5.1\n", - "IPython 4.0.3\n" - ] + "collapsed": false, + "jupyter": { + "outputs_hidden": false } - ], + }, + "outputs": [], "source": [ "%watermark -a 'Sebastian Raschka' -v -d" ] @@ -166,7 +161,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Now, the tricky part is that we have multiple independent namespaces in Python, and names can be reused for different namespaces (only the objects are unique, for example:\n", + "Now, the tricky part is that we have multiple independent namespaces in Python, and names can be reused for different namespaces. Only the objects in namespaces are unique, for example:\n", "\n", "
a_namespace = {'name_a':object_1, 'name_b':object_2, ...}\n",
     "b_namespace = {'name_a':object_3, 'name_b':object_4, ...}
\n", @@ -191,20 +186,14 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "1 global\n", - "5 in foo()\n" - ] + "collapsed": false, + "jupyter": { + "outputs_hidden": false } - ], + }, + "outputs": [], "source": [ "i = 1\n", "\n", @@ -250,21 +239,14 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": null, "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "loc in foo(): True\n", - "loc in global: False\n", - "glob in global: True\n" - ] + "collapsed": false, + "jupyter": { + "outputs_hidden": false } - ], + }, + "outputs": [], "source": [ "#print(globals()) # prints global namespace\n", "#print(locals()) # prints local namespace\n", @@ -277,7 +259,8 @@ "\n", "foo()\n", "print('loc in global:', 'loc' in globals()) \n", - "print('glob in global:', 'foo' in globals())" + "print('glob in global:', 'glob' in globals())\n", + "print('foo in global:', 'foo' in globals())" ] }, { @@ -299,7 +282,7 @@ "where the arrows should denote the direction of the namespace-hierarchy search order. \n", "\n", "- *Local* can be inside a function or class method, for example. \n", - "- *Enclosed* can be its `enclosing` function, e.g., if a function is wrapped inside another function. \n", + "- *Enclosed* can be variables of an`enclosing` function, e.g., if a function is wrapped inside another function. \n", "- *Global* refers to the uppermost level of the executing script itself, and \n", "- *Built-in* are special names that Python reserves for itself. \n", "\n", @@ -311,21 +294,14 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": null, "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "3.141592653589793 from the math module\n", - "3.141592653589793 from the numpy package\n", - "3.141592653589793 from the scipy package\n" - ] + "collapsed": false, + "jupyter": { + "outputs_hidden": false } - ], + }, + "outputs": [], "source": [ "import numpy\n", "import math\n", @@ -347,11 +323,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "
\n", - "
\n", - "![LEGB figure](https://raw.githubusercontent.com/rasbt/python_reference/master/Images/scope_resolution_1.png)\n", - "
\n", - "
" + "![LEGB figure](https://raw.githubusercontent.com/rasbt/python_reference/master/Images/scope_resolution_1.png)" ] }, { @@ -381,9 +353,12 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "metadata": { - "collapsed": false + "collapsed": false, + "jupyter": { + "outputs_hidden": false + } }, "outputs": [], "source": [ @@ -427,7 +402,7 @@ "source": [ "### Here is why:\n", "\n", - "We call `a_func()` first, which is supposed to print the value of `a_var`. According to the LEGB rule, the function will first look in its own local scope (L) if `a_var` is defined there. Since `a_func()` does not define its own `a_var`, it will look one-level above in the global scope (G) in which `a_var` has been defined previously.\n", + "We call `a_func()` first, which is supposed to print the value of `a_var`. According to the LEGB rule, the function will first look in its own local scope (L) if `a_var` is defined there. Since `a_func()` does not define its own `a_var`, it will look two levels above (Local-> Enclosed -> Global) in the global scope (G) in which `a_var` has been defined previously.\n", "
\n", "
" ] @@ -443,9 +418,12 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "metadata": { - "collapsed": false + "collapsed": false, + "jupyter": { + "outputs_hidden": false + } }, "outputs": [], "source": [ @@ -501,21 +479,14 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": null, "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "global value [ a_var outside a_func() ]\n", - "local value [ a_var inside a_func() ]\n", - "local value [ a_var outside a_func() ]\n" - ] + "collapsed": false, + "jupyter": { + "outputs_hidden": false } - ], + }, + "outputs": [], "source": [ "a_var = 'global value'\n", "\n", @@ -538,30 +509,14 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": null, "metadata": { - "collapsed": false - }, - "outputs": [ - { - "ename": "UnboundLocalError", - "evalue": "local variable 'a_var' referenced before assignment", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mUnboundLocalError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma_var\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'[ a_var outside a_func() ]'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 8\u001b[0;31m \u001b[0ma_func\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[0;32m\u001b[0m in \u001b[0;36ma_func\u001b[0;34m()\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0ma_func\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0ma_var\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0ma_var\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 5\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma_var\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'[ a_var inside a_func() ]'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;31mUnboundLocalError\u001b[0m: local variable 'a_var' referenced before assignment" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "1 [ a_var outside a_func() ]\n" - ] + "collapsed": false, + "jupyter": { + "outputs_hidden": false } - ], + }, + "outputs": [], "source": [ "a_var = 1\n", "\n", @@ -573,6 +528,30 @@ "a_func()" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This is how to make the above code work:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "a_var = 1\n", + "\n", + "def a_func():\n", + " global a_var\n", + " a_var = a_var + 1\n", + " print(a_var, '[ a_var inside a_func() ]')\n", + "\n", + "print(a_var, '[ a_var outside a_func() ]')\n", + "a_func()" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -610,9 +589,12 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": null, "metadata": { - "collapsed": false + "collapsed": false, + "jupyter": { + "outputs_hidden": false + } }, "outputs": [], "source": [ @@ -657,7 +639,7 @@ "source": [ "### Here is why:\n", "\n", - "Let us quickly recapitulate what we just did: We called `outer()`, which defined the variable `a_var` locally (next to an existing `a_var` in the global scope). Next, the `outer()` function called `inner()`, which in turn defined a variable with of name `a_var` as well. The `print()` function inside `inner()` searched in the local scope first (L->E) before it went up in the scope hierarchy, and therefore it printed the value that was assigned in the local scope." + "Let us quickly recapitulate what we just did: We called `outer()`, which defined the variable `a_var` locally (next to an existing `a_var` in the global scope). Next, the `outer()` function called `inner()`, which in turn defined a variable with of name `a_var` as well. The `print()` function inside `inner()` searched in the local scope first (L) before it went up in the scope hierarchy, and therefore it printed the value that was assigned in the local scope." ] }, { @@ -670,21 +652,14 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": null, "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "outer before: local value\n", - "in inner(): inner value\n", - "outer after: inner value\n" - ] + "collapsed": false, + "jupyter": { + "outputs_hidden": false } - ], + }, + "outputs": [], "source": [ "a_var = 'global value'\n", "\n", @@ -715,7 +690,7 @@ "source": [ "## 3. LEGB - Local, Enclosed, Global, Built-in\n", "\n", - "To wrap up the LEGB rule, let us come to the built-in scope. Here, we will define our \"own\" length-funcion, which happens to bear the same name as the in-built `len()` function. What outcome do you excpect if we'd execute the following code?" + "To wrap up the LEGB rule, let us come to the built-in scope. Here, we will define our \"own\" length-funcion, which happens to bear the same name as the in-built `len()` function. What outcome do you expect if we'd execute the following code?" ] }, { @@ -727,9 +702,12 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": null, "metadata": { - "collapsed": false + "collapsed": false, + "jupyter": { + "outputs_hidden": false + } }, "outputs": [], "source": [ @@ -805,9 +783,12 @@ }, { "cell_type": "code", - "execution_count": 59, + "execution_count": null, "metadata": { - "collapsed": false + "collapsed": false, + "jupyter": { + "outputs_hidden": false + } }, "outputs": [], "source": [ @@ -887,19 +868,14 @@ }, { "cell_type": "code", - "execution_count": 42, + "execution_count": null, "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "8\n" - ] + "collapsed": false, + "jupyter": { + "outputs_hidden": false } - ], + }, + "outputs": [], "source": [ "a_var = 2\n", "\n", @@ -930,9 +906,12 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": null, "metadata": { - "collapsed": false + "collapsed": false, + "jupyter": { + "outputs_hidden": false + } }, "outputs": [], "source": [ @@ -941,9 +920,12 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": null, "metadata": { - "collapsed": false + "collapsed": false, + "jupyter": { + "outputs_hidden": false + } }, "outputs": [], "source": [ @@ -952,9 +934,12 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": null, "metadata": { - "collapsed": false + "collapsed": false, + "jupyter": { + "outputs_hidden": false + } }, "outputs": [], "source": [ @@ -963,9 +948,12 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": null, "metadata": { - "collapsed": false + "collapsed": false, + "jupyter": { + "outputs_hidden": false + } }, "outputs": [], "source": [ @@ -974,9 +962,12 @@ }, { "cell_type": "code", - "execution_count": 58, + "execution_count": null, "metadata": { - "collapsed": false + "collapsed": false, + "jupyter": { + "outputs_hidden": false + } }, "outputs": [], "source": [ @@ -1024,20 +1015,14 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": null, "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "4 -> a in for-loop\n", - "4 -> a in global\n" - ] + "collapsed": false, + "jupyter": { + "outputs_hidden": false } - ], + }, + "outputs": [], "source": [ "for a in range(5):\n", " if a == 4:\n", @@ -1054,20 +1039,14 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": null, "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "4 -> b in for-loop\n", - "4 -> b in global\n" - ] + "collapsed": false, + "jupyter": { + "outputs_hidden": false } - ], + }, + "outputs": [], "source": [ "b = 1\n", "for b in range(5):\n", @@ -1085,20 +1064,14 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[0, 1, 2, 3, 4]\n", - "1 -> i in global\n" - ] + "collapsed": false, + "jupyter": { + "outputs_hidden": false } - ], + }, + "outputs": [], "source": [ "i = 1\n", "print([i for i in range(5)])\n", @@ -1124,15 +1097,6 @@ "\n", "\"List comprehensions no longer support the syntactic form `[... for var in item1, item2, ...]`. Use `[... for var in (item1, item2, ...)]` instead. Also note that list comprehensions have different semantics: they are closer to syntactic sugar for a generator expression inside a `list()` constructor, and in particular the loop control variables are no longer leaked into the surrounding scope.\"" ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [] } ], "metadata": { @@ -1151,9 +1115,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.5.0" + "version": "3.8.2" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 4 }