From dd25efd41f33986e92180e7982e1502ad183f744 Mon Sep 17 00:00:00 2001 From: Sebastian Raschka Date: Tue, 29 Apr 2014 21:55:50 -0400 Subject: [PATCH] fixed for-loop example --- tutorials/scope_resolution_legb_rule.ipynb | 63 ++++++++++++++-------- 1 file changed, 42 insertions(+), 21 deletions(-) diff --git a/tutorials/scope_resolution_legb_rule.ipynb b/tutorials/scope_resolution_legb_rule.ipynb index 57d4490..1ad6b74 100644 --- a/tutorials/scope_resolution_legb_rule.ipynb +++ b/tutorials/scope_resolution_legb_rule.ipynb @@ -176,7 +176,7 @@ ] } ], - "prompt_number": 4 + "prompt_number": 1 }, { "cell_type": "markdown", @@ -200,6 +200,47 @@ "So, how does Python now which namespace it has to search if we want to print the value of the variable `i`? This is where Python's LEGB-rule comes into play, which we will discuss in the next section." ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Tip:\n", + "If we want to print out the dictionary mapping of the global and local variables, we can use the\n", + "the functions `global()` and `local()" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#print(globals()) # prints global namespace\n", + "#print(locals()) # prints local namespace\n", + "\n", + "glob = 1\n", + "\n", + "def foo():\n", + " loc = 5\n", + " print('loc in foo():', 'loc' in locals())\n", + "\n", + "foo()\n", + "print('loc in global:', 'loc' in globals()) \n", + "print('glob in global:', 'foo' in globals())" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "loc in foo(): True\n", + "loc in global: False\n", + "glob in global: True\n" + ] + } + ], + "prompt_number": 11 + }, { "cell_type": "markdown", "metadata": {}, @@ -490,26 +531,6 @@ "
" ] }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Tip:\n", - "If we want to print out the dictionary mapping of the global and local variables, we can use the\n", - "the functions `global()` and `local()" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "print(globals()) # prints global namespace\n", - "print(locals()) # prints local namespace" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, { "cell_type": "markdown", "metadata": {},