Skip to content

Commit c3fc839

Browse files
committed
fixed for-loop example
1 parent 2587ebf commit c3fc839

1 file changed

Lines changed: 14 additions & 25 deletions

File tree

tutorials/scope_resolution_legb_rule.ipynb

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -947,7 +947,7 @@
947947
"cell_type": "markdown",
948948
"metadata": {},
949949
"source": [
950-
"As it was briefly mentioned in the introduction, `for-loops` will create their own namespaces, which will be deleted after the for-loop has completed. Consider the following example (exectuted in Python 3.4):\n"
950+
"In contrast to some other programming languages, `for-loops` will use the scope they exist in and leave their defined loop-variable behind.\n"
951951
]
952952
},
953953
{
@@ -971,32 +971,13 @@
971971
]
972972
}
973973
],
974-
"prompt_number": 15
975-
},
976-
{
977-
"cell_type": "markdown",
978-
"metadata": {},
979-
"source": [
980-
"We can roughly sketch the situation as follows \n",
981-
"\n",
982-
"*before the for-loop*: \n",
983-
"`- global_namespace = {'a_name':object1, ...}`\n",
984-
"\n",
985-
"*during the for-loop*: \n",
986-
"`- global_namespace = {'a_name':object1, ...}` \n",
987-
"`- for-loop_namespace = {'a':object201, ...}`\n",
988-
"\n",
989-
"*after the for-loop*: \n",
990-
"`- global_namespace = {'a_name':object1, ...}`\n",
991-
"\n",
992-
"\n"
993-
]
974+
"prompt_number": 5
994975
},
995976
{
996977
"cell_type": "markdown",
997978
"metadata": {},
998979
"source": [
999-
"**However, this does not apply if we defined the `for-loop` variable in the global namespace before!**"
980+
"**This also applies if we explicitely defined the `for-loop` variable in the global namespace before!** In this case it will rebind the existing variable:"
1000981
]
1001982
},
1002983
{
@@ -1021,13 +1002,13 @@
10211002
]
10221003
}
10231004
],
1024-
"prompt_number": 7
1005+
"prompt_number": 9
10251006
},
10261007
{
10271008
"cell_type": "markdown",
10281009
"metadata": {},
10291010
"source": [
1030-
"In **Python 3.4**, we can use closures to prevent the for-loop variable to cut into the global namespace. Here is an example (exectuted in Python 3.4):"
1011+
"However, in **Python 3.x**, we can use closures to prevent the for-loop variable to cut into the global namespace. Here is an example (exectuted in Python 3.4):"
10311012
]
10321013
},
10331014
{
@@ -1056,12 +1037,20 @@
10561037
"cell_type": "markdown",
10571038
"metadata": {},
10581039
"source": [
1059-
"Why did I mention \"Python 3.4\"? Well, as it happens, the same code executed in Python 2.x would print:\n",
1040+
"Why did I mention \"Python 3.x\"? Well, as it happens, the same code executed in Python 2.x would print:\n",
10601041
"\n",
10611042
"<pre>\n",
10621043
"print(4, '-> i in global')\n",
10631044
"<pre>"
10641045
]
1046+
},
1047+
{
1048+
"cell_type": "code",
1049+
"collapsed": false,
1050+
"input": [],
1051+
"language": "python",
1052+
"metadata": {},
1053+
"outputs": []
10651054
}
10661055
],
10671056
"metadata": {}

0 commit comments

Comments
 (0)