Skip to content

Commit 9f22042

Browse files
author
Sebastian Raschka
committed
fixes and new section
1 parent a0edd6b commit 9f22042

1 file changed

Lines changed: 113 additions & 20 deletions

File tree

not_so_obvious_python_stuff.ipynb

Lines changed: 113 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"metadata": {
3-
"name": "",
3+
"name": "not_so_obvious_python_stuff",
44
"signature": "sha256:9654ab25e9d989ee8f196ec7cd5c2b7222157dbe195ddf70e6ec34329c9db545"
55
},
66
"nbformat": 3,
@@ -80,7 +80,8 @@
8080
"- [Key differences between Python 2 and 3](#python_differences)\n",
8181
"- [Function annotations - What are those `->`'s in my Python code?](#function_annotation)\n",
8282
"- [Abortive statements in `finally` blocks](#finally_blocks)\n",
83-
"- [Assigning types to variables as values](#variable_types)"
83+
"- [Assigning types to variables as values](#variable_types)\n",
84+
"- [Only the first clause of generators is evaluated immediately](#generator_rhs)"
8485
]
8586
},
8687
{
@@ -274,13 +275,13 @@
274275
"collapsed": false,
275276
"input": [
276277
"a_list = []\n",
277-
"print('ID:',id(a_list))\n",
278+
"print(a_list, '\\nID (initial):',id(a_list), '\\n')\n",
278279
"\n",
279280
"a_list.append(1)\n",
280-
"print('ID (append):',id(a_list))\n",
281+
"print(a_list, '\\nID (append):',id(a_list), '\\n')\n",
281282
"\n",
282-
"a_list.append(2)\n",
283-
"print('ID (extend):',id(a_list))"
283+
"a_list.extend([2])\n",
284+
"print(a_list, '\\nID (extend):',id(a_list))"
284285
],
285286
"language": "python",
286287
"metadata": {},
@@ -289,13 +290,18 @@
289290
"output_type": "stream",
290291
"stream": "stdout",
291292
"text": [
292-
"ID: 4366495544\n",
293-
"ID (append): 4366495544\n",
294-
"ID (extend): 4366495544\n"
293+
"[] \n",
294+
"ID (initial): 140704077653128 \n",
295+
"\n",
296+
"[1] \n",
297+
"ID (append): 140704077653128 \n",
298+
"\n",
299+
"[1, 2] \n",
300+
"ID (extend): 140704077653128\n"
295301
]
296302
}
297303
],
298-
"prompt_number": 7
304+
"prompt_number": 6
299305
},
300306
{
301307
"cell_type": "code",
@@ -2983,44 +2989,131 @@
29832989
"cell_type": "markdown",
29842990
"metadata": {},
29852991
"source": [
2986-
"<a name=\"changelog\"></a>\n",
2987-
"<br>\n",
29882992
"<br>\n",
29892993
"<br>\n",
2990-
"<br>\n"
2994+
"<a name='generator_rhs'>"
29912995
]
29922996
},
29932997
{
29942998
"cell_type": "markdown",
29952999
"metadata": {},
29963000
"source": [
2997-
"# Changelog"
3001+
"# Only the first clause of generators is evaluated immediately"
29983002
]
29993003
},
30003004
{
30013005
"cell_type": "markdown",
30023006
"metadata": {},
30033007
"source": [
3004-
"[[back to top](#sections)]"
3008+
"The main reason why we love to use generators in certain cases (i.e., when we are dealing with large numbers of computations) is that it only computes the next value when it is needed, which is also known as \"lazy\" evaluation.\n",
3009+
"However, the first clause of an generator is already checked upon it's creation, as the following example demonstrates:"
30053010
]
30063011
},
3012+
{
3013+
"cell_type": "code",
3014+
"collapsed": false,
3015+
"input": [
3016+
"gen_fails = (i for i in 1/0)"
3017+
],
3018+
"language": "python",
3019+
"metadata": {},
3020+
"outputs": [
3021+
{
3022+
"ename": "ZeroDivisionError",
3023+
"evalue": "division by zero",
3024+
"output_type": "pyerr",
3025+
"traceback": [
3026+
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mZeroDivisionError\u001b[0m Traceback (most recent call last)",
3027+
"\u001b[0;32m<ipython-input-18-29312e1ece8d>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mgen_fails\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mi\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[0;32min\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m/\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
3028+
"\u001b[0;31mZeroDivisionError\u001b[0m: division by zero"
3029+
]
3030+
}
3031+
],
3032+
"prompt_number": 18
3033+
},
30073034
{
30083035
"cell_type": "markdown",
30093036
"metadata": {},
30103037
"source": [
3011-
"#### 04/27/2014\n",
3012-
"- minor fixes of typos\n",
3013-
"- single- vs. double-underscore clarification in the private class section."
3038+
"Certainly, this is a nice feature, since it notifies us about syntax erros immediately. However, this is (unfortunately) not the case if we have multiple cases in our generator."
30143039
]
30153040
},
30163041
{
30173042
"cell_type": "code",
30183043
"collapsed": false,
3019-
"input": [],
3044+
"input": [
3045+
"gen_succeeds = (i for i in range(5) for j in 1/0)"
3046+
],
30203047
"language": "python",
30213048
"metadata": {},
30223049
"outputs": [],
3023-
"prompt_number": 1
3050+
"prompt_number": 19
3051+
},
3052+
{
3053+
"cell_type": "code",
3054+
"collapsed": false,
3055+
"input": [
3056+
"print('But obviously fails when we iterate ...')\n",
3057+
"for i in gen_succeeds:\n",
3058+
" print(i)"
3059+
],
3060+
"language": "python",
3061+
"metadata": {},
3062+
"outputs": [
3063+
{
3064+
"ename": "ZeroDivisionError",
3065+
"evalue": "division by zero",
3066+
"output_type": "pyerr",
3067+
"traceback": [
3068+
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mZeroDivisionError\u001b[0m Traceback (most recent call last)",
3069+
"\u001b[0;32m<ipython-input-20-8a83a1022971>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'But obviously fails when we iterate ...'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mgen_succeeds\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
3070+
"\u001b[0;32m<ipython-input-19-c54c53f2218a>\u001b[0m in \u001b[0;36m<genexpr>\u001b[0;34m(.0)\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mgen_succeeds\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mi\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m5\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mj\u001b[0m \u001b[0;32min\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m/\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
3071+
"\u001b[0;31mZeroDivisionError\u001b[0m: division by zero"
3072+
]
3073+
},
3074+
{
3075+
"output_type": "stream",
3076+
"stream": "stdout",
3077+
"text": [
3078+
"But obviously fails when we iterate ...\n"
3079+
]
3080+
}
3081+
],
3082+
"prompt_number": 20
3083+
},
3084+
{
3085+
"cell_type": "markdown",
3086+
"metadata": {},
3087+
"source": [
3088+
"<a name=\"changelog\"></a>\n",
3089+
"<br>\n",
3090+
"<br>\n",
3091+
"<br>\n",
3092+
"<br>\n"
3093+
]
3094+
},
3095+
{
3096+
"cell_type": "markdown",
3097+
"metadata": {},
3098+
"source": [
3099+
"# Changelog"
3100+
]
3101+
},
3102+
{
3103+
"cell_type": "markdown",
3104+
"metadata": {},
3105+
"source": [
3106+
"[[back to top](#sections)]"
3107+
]
3108+
},
3109+
{
3110+
"cell_type": "markdown",
3111+
"metadata": {},
3112+
"source": [
3113+
"#### 04/27/2014\n",
3114+
"- minor fixes of typos \n",
3115+
"- new section: \"Only the first clause of generators is evaluated immediately\""
3116+
]
30243117
},
30253118
{
30263119
"cell_type": "code",

0 commit comments

Comments
 (0)