From cb5853e6daa293ef3c69ed4432869e7dd4010680 Mon Sep 17 00:00:00 2001 From: rasbt Date: Fri, 2 May 2014 16:14:09 -0400 Subject: [PATCH] output fix for for-loop leak --- ...t_so_obvious_python_stuff-checkpoint.ipynb | 57 ++++++++++++++++++- ...cope_resolution_legb_rule-checkpoint.ipynb | 10 +++- not_so_obvious_python_stuff.ipynb | 13 ++++- tutorials/scope_resolution_legb_rule.ipynb | 10 +++- 4 files changed, 85 insertions(+), 5 deletions(-) diff --git a/.ipynb_checkpoints/not_so_obvious_python_stuff-checkpoint.ipynb b/.ipynb_checkpoints/not_so_obvious_python_stuff-checkpoint.ipynb index 9c32267..be8ba25 100644 --- a/.ipynb_checkpoints/not_so_obvious_python_stuff-checkpoint.ipynb +++ b/.ipynb_checkpoints/not_so_obvious_python_stuff-checkpoint.ipynb @@ -1,7 +1,7 @@ { "metadata": { "name": "", - "signature": "sha256:70671a8e3f32d21b27867ecdc6d80f3b5c10f00339a2fae3860c4db8930dc7c6" + "signature": "sha256:3b7a6d43400c23d25d965b726e6fba3db0b9b0d2d0bbad73c96b8857f8eaa7ee" }, "nbformat": 3, "nbformat_minor": 0, @@ -2577,6 +2577,52 @@ "metadata": {}, "outputs": [] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### In Python 3.x for-loop variables don't leak into the global namespace anymore" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This goes back to a change that was made in Python 3.x and is described in [What\u2019s New In Python 3.0](https://docs.python.org/3/whatsnew/3.0.html) as follows:\n", + "\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", + "collapsed": false, + "input": [ + "i = 1\n", + "print([i for i in range(5)])\n", + "print(i, '-> i in global')" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "[0, 1, 2, 3, 4]\n", + "1 -> i in global\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In Python 2.x this would print \n", + "`4 -> i in global`" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -3345,6 +3391,7 @@ "metadata": {}, "source": [ "#### 05/02/2014\n", + "- new section in Python 3.x and Python 2.x key differences: for-loop leak<\n", "- new section: Metaclasses - What creates a new instance of a class? \n", "\n", "#### 05/01/2014\n", @@ -3354,6 +3401,14 @@ "- minor fixes of typos \n", "- new section: \"Only the first clause of generators is evaluated immediately\"" ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [], + "language": "python", + "metadata": {}, + "outputs": [] } ], "metadata": {} diff --git a/.ipynb_checkpoints/scope_resolution_legb_rule-checkpoint.ipynb b/.ipynb_checkpoints/scope_resolution_legb_rule-checkpoint.ipynb index f1924c9..70cbf9d 100644 --- a/.ipynb_checkpoints/scope_resolution_legb_rule-checkpoint.ipynb +++ b/.ipynb_checkpoints/scope_resolution_legb_rule-checkpoint.ipynb @@ -1,7 +1,7 @@ { "metadata": { "name": "", - "signature": "sha256:1e3a847a38ee33d41e745f74fe6b8a143b9aeb47d0a1e3029ea52899d97687cb" + "signature": "sha256:eb9ececefb4c35b16d0496c7f613e4a64bb35b2e9f79b7e049c5b434bd2e6654" }, "nbformat": 3, "nbformat_minor": 0, @@ -1076,6 +1076,14 @@ "\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", + "collapsed": false, + "input": [], + "language": "python", + "metadata": {}, + "outputs": [] } ], "metadata": {} diff --git a/not_so_obvious_python_stuff.ipynb b/not_so_obvious_python_stuff.ipynb index a17f6dc..be8ba25 100644 --- a/not_so_obvious_python_stuff.ipynb +++ b/not_so_obvious_python_stuff.ipynb @@ -1,7 +1,7 @@ { "metadata": { "name": "", - "signature": "sha256:08b042706cb433ce1f2446c6b6ac00be197268042e605da41164e7d02562766d" + "signature": "sha256:3b7a6d43400c23d25d965b726e6fba3db0b9b0d2d0bbad73c96b8857f8eaa7ee" }, "nbformat": 3, "nbformat_minor": 0, @@ -2620,7 +2620,7 @@ "metadata": {}, "source": [ "In Python 2.x this would print \n", - "`print(4, '-> i in global')`" + "`4 -> i in global`" ] }, { @@ -3391,6 +3391,7 @@ "metadata": {}, "source": [ "#### 05/02/2014\n", + "- new section in Python 3.x and Python 2.x key differences: for-loop leak<\n", "- new section: Metaclasses - What creates a new instance of a class? \n", "\n", "#### 05/01/2014\n", @@ -3400,6 +3401,14 @@ "- minor fixes of typos \n", "- new section: \"Only the first clause of generators is evaluated immediately\"" ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [], + "language": "python", + "metadata": {}, + "outputs": [] } ], "metadata": {} diff --git a/tutorials/scope_resolution_legb_rule.ipynb b/tutorials/scope_resolution_legb_rule.ipynb index f1924c9..70cbf9d 100644 --- a/tutorials/scope_resolution_legb_rule.ipynb +++ b/tutorials/scope_resolution_legb_rule.ipynb @@ -1,7 +1,7 @@ { "metadata": { "name": "", - "signature": "sha256:1e3a847a38ee33d41e745f74fe6b8a143b9aeb47d0a1e3029ea52899d97687cb" + "signature": "sha256:eb9ececefb4c35b16d0496c7f613e4a64bb35b2e9f79b7e049c5b434bd2e6654" }, "nbformat": 3, "nbformat_minor": 0, @@ -1076,6 +1076,14 @@ "\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", + "collapsed": false, + "input": [], + "language": "python", + "metadata": {}, + "outputs": [] } ], "metadata": {}