mirror of
https://github.com/rasbt/python_reference.git
synced 2025-04-17 02:07:36 +00:00
output fix for for-loop leak
This commit is contained in:
parent
afacae6cc4
commit
3c42a45c60
1084
.ipynb_checkpoints/scope_resolution_legb_rule-checkpoint.ipynb
Normal file
1084
.ipynb_checkpoints/scope_resolution_legb_rule-checkpoint.ipynb
Normal file
File diff suppressed because it is too large
Load Diff
@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"name": "",
|
"name": "",
|
||||||
"signature": "sha256:70671a8e3f32d21b27867ecdc6d80f3b5c10f00339a2fae3860c4db8930dc7c6"
|
"signature": "sha256:08b042706cb433ce1f2446c6b6ac00be197268042e605da41164e7d02562766d"
|
||||||
},
|
},
|
||||||
"nbformat": 3,
|
"nbformat": 3,
|
||||||
"nbformat_minor": 0,
|
"nbformat_minor": 0,
|
||||||
@ -2577,6 +2577,52 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": []
|
"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",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
{
|
{
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"name": "scope_resolution_legb_rule"
|
"name": "",
|
||||||
|
"signature": "sha256:1e3a847a38ee33d41e745f74fe6b8a143b9aeb47d0a1e3029ea52899d97687cb"
|
||||||
},
|
},
|
||||||
"nbformat": 3,
|
"nbformat": 3,
|
||||||
"nbformat_minor": 0,
|
"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",
|
"Why did I mention \"Python 3.x\"? Well, as it happens, the same code executed in Python 2.x would print:\n",
|
||||||
"\n",
|
"\n",
|
||||||
"<pre>\n",
|
"<pre>\n",
|
||||||
"print(4, '-> i in global')\n",
|
"4 -> i in global\n",
|
||||||
"<pre>"
|
"<pre>"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
Loading…
x
Reference in New Issue
Block a user