|
947 | 947 | "cell_type": "markdown", |
948 | 948 | "metadata": {}, |
949 | 949 | "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" |
951 | 951 | ] |
952 | 952 | }, |
953 | 953 | { |
|
971 | 971 | ] |
972 | 972 | } |
973 | 973 | ], |
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 |
994 | 975 | }, |
995 | 976 | { |
996 | 977 | "cell_type": "markdown", |
997 | 978 | "metadata": {}, |
998 | 979 | "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:" |
1000 | 981 | ] |
1001 | 982 | }, |
1002 | 983 | { |
|
1021 | 1002 | ] |
1022 | 1003 | } |
1023 | 1004 | ], |
1024 | | - "prompt_number": 7 |
| 1005 | + "prompt_number": 9 |
1025 | 1006 | }, |
1026 | 1007 | { |
1027 | 1008 | "cell_type": "markdown", |
1028 | 1009 | "metadata": {}, |
1029 | 1010 | "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):" |
1031 | 1012 | ] |
1032 | 1013 | }, |
1033 | 1014 | { |
|
1056 | 1037 | "cell_type": "markdown", |
1057 | 1038 | "metadata": {}, |
1058 | 1039 | "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", |
1060 | 1041 | "\n", |
1061 | 1042 | "<pre>\n", |
1062 | 1043 | "print(4, '-> i in global')\n", |
1063 | 1044 | "<pre>" |
1064 | 1045 | ] |
| 1046 | + }, |
| 1047 | + { |
| 1048 | + "cell_type": "code", |
| 1049 | + "collapsed": false, |
| 1050 | + "input": [], |
| 1051 | + "language": "python", |
| 1052 | + "metadata": {}, |
| 1053 | + "outputs": [] |
1065 | 1054 | } |
1066 | 1055 | ], |
1067 | 1056 | "metadata": {} |
|
0 commit comments