|
1 | 1 | { |
2 | 2 | "metadata": { |
3 | | - "name": "", |
| 3 | + "name": "not_so_obvious_python_stuff", |
4 | 4 | "signature": "sha256:9654ab25e9d989ee8f196ec7cd5c2b7222157dbe195ddf70e6ec34329c9db545" |
5 | 5 | }, |
6 | 6 | "nbformat": 3, |
|
80 | 80 | "- [Key differences between Python 2 and 3](#python_differences)\n", |
81 | 81 | "- [Function annotations - What are those `->`'s in my Python code?](#function_annotation)\n", |
82 | 82 | "- [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)" |
84 | 85 | ] |
85 | 86 | }, |
86 | 87 | { |
|
274 | 275 | "collapsed": false, |
275 | 276 | "input": [ |
276 | 277 | "a_list = []\n", |
277 | | - "print('ID:',id(a_list))\n", |
| 278 | + "print(a_list, '\\nID (initial):',id(a_list), '\\n')\n", |
278 | 279 | "\n", |
279 | 280 | "a_list.append(1)\n", |
280 | | - "print('ID (append):',id(a_list))\n", |
| 281 | + "print(a_list, '\\nID (append):',id(a_list), '\\n')\n", |
281 | 282 | "\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))" |
284 | 285 | ], |
285 | 286 | "language": "python", |
286 | 287 | "metadata": {}, |
|
289 | 290 | "output_type": "stream", |
290 | 291 | "stream": "stdout", |
291 | 292 | "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" |
295 | 301 | ] |
296 | 302 | } |
297 | 303 | ], |
298 | | - "prompt_number": 7 |
| 304 | + "prompt_number": 6 |
299 | 305 | }, |
300 | 306 | { |
301 | 307 | "cell_type": "code", |
|
2983 | 2989 | "cell_type": "markdown", |
2984 | 2990 | "metadata": {}, |
2985 | 2991 | "source": [ |
2986 | | - "<a name=\"changelog\"></a>\n", |
2987 | | - "<br>\n", |
2988 | 2992 | "<br>\n", |
2989 | 2993 | "<br>\n", |
2990 | | - "<br>\n" |
| 2994 | + "<a name='generator_rhs'>" |
2991 | 2995 | ] |
2992 | 2996 | }, |
2993 | 2997 | { |
2994 | 2998 | "cell_type": "markdown", |
2995 | 2999 | "metadata": {}, |
2996 | 3000 | "source": [ |
2997 | | - "# Changelog" |
| 3001 | + "# Only the first clause of generators is evaluated immediately" |
2998 | 3002 | ] |
2999 | 3003 | }, |
3000 | 3004 | { |
3001 | 3005 | "cell_type": "markdown", |
3002 | 3006 | "metadata": {}, |
3003 | 3007 | "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:" |
3005 | 3010 | ] |
3006 | 3011 | }, |
| 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 | + }, |
3007 | 3034 | { |
3008 | 3035 | "cell_type": "markdown", |
3009 | 3036 | "metadata": {}, |
3010 | 3037 | "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." |
3014 | 3039 | ] |
3015 | 3040 | }, |
3016 | 3041 | { |
3017 | 3042 | "cell_type": "code", |
3018 | 3043 | "collapsed": false, |
3019 | | - "input": [], |
| 3044 | + "input": [ |
| 3045 | + "gen_succeeds = (i for i in range(5) for j in 1/0)" |
| 3046 | + ], |
3020 | 3047 | "language": "python", |
3021 | 3048 | "metadata": {}, |
3022 | 3049 | "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 | + ] |
3024 | 3117 | }, |
3025 | 3118 | { |
3026 | 3119 | "cell_type": "code", |
|
0 commit comments