output fix for for-loop leak

This commit is contained in:
rasbt 2014-05-02 16:10:00 -04:00
parent afacae6cc4
commit 3c42a45c60
3 changed files with 1134 additions and 3 deletions

File diff suppressed because it is too large Load Diff

View File

@ -1,7 +1,7 @@
{
"metadata": {
"name": "",
"signature": "sha256:70671a8e3f32d21b27867ecdc6d80f3b5c10f00339a2fae3860c4db8930dc7c6"
"signature": "sha256:08b042706cb433ce1f2446c6b6ac00be197268042e605da41164e7d02562766d"
},
"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",
"`print(4, '-> i in global')`"
]
},
{
"cell_type": "markdown",
"metadata": {},

View File

@ -1,6 +1,7 @@
{
"metadata": {
"name": "scope_resolution_legb_rule"
"name": "",
"signature": "sha256:1e3a847a38ee33d41e745f74fe6b8a143b9aeb47d0a1e3029ea52899d97687cb"
},
"nbformat": 3,
"nbformat_minor": 0,
@ -1063,7 +1064,7 @@
"Why did I mention \"Python 3.x\"? Well, as it happens, the same code executed in Python 2.x would print:\n",
"\n",
"<pre>\n",
"print(4, '-> i in global')\n",
"4 -> i in global\n",
"<pre>"
]
},