fixes and new section

This commit is contained in:
Sebastian Raschka 2014-04-28 00:13:27 -04:00
parent a0edd6b293
commit 9f22042812

View File

@ -1,6 +1,6 @@
{ {
"metadata": { "metadata": {
"name": "", "name": "not_so_obvious_python_stuff",
"signature": "sha256:9654ab25e9d989ee8f196ec7cd5c2b7222157dbe195ddf70e6ec34329c9db545" "signature": "sha256:9654ab25e9d989ee8f196ec7cd5c2b7222157dbe195ddf70e6ec34329c9db545"
}, },
"nbformat": 3, "nbformat": 3,
@ -80,7 +80,8 @@
"- [Key differences between Python 2 and 3](#python_differences)\n", "- [Key differences between Python 2 and 3](#python_differences)\n",
"- [Function annotations - What are those `->`'s in my Python code?](#function_annotation)\n", "- [Function annotations - What are those `->`'s in my Python code?](#function_annotation)\n",
"- [Abortive statements in `finally` blocks](#finally_blocks)\n", "- [Abortive statements in `finally` blocks](#finally_blocks)\n",
"- [Assigning types to variables as values](#variable_types)" "- [Assigning types to variables as values](#variable_types)\n",
"- [Only the first clause of generators is evaluated immediately](#generator_rhs)"
] ]
}, },
{ {
@ -274,13 +275,13 @@
"collapsed": false, "collapsed": false,
"input": [ "input": [
"a_list = []\n", "a_list = []\n",
"print('ID:',id(a_list))\n", "print(a_list, '\\nID (initial):',id(a_list), '\\n')\n",
"\n", "\n",
"a_list.append(1)\n", "a_list.append(1)\n",
"print('ID (append):',id(a_list))\n", "print(a_list, '\\nID (append):',id(a_list), '\\n')\n",
"\n", "\n",
"a_list.append(2)\n", "a_list.extend([2])\n",
"print('ID (extend):',id(a_list))" "print(a_list, '\\nID (extend):',id(a_list))"
], ],
"language": "python", "language": "python",
"metadata": {}, "metadata": {},
@ -289,13 +290,18 @@
"output_type": "stream", "output_type": "stream",
"stream": "stdout", "stream": "stdout",
"text": [ "text": [
"ID: 4366495544\n", "[] \n",
"ID (append): 4366495544\n", "ID (initial): 140704077653128 \n",
"ID (extend): 4366495544\n" "\n",
"[1] \n",
"ID (append): 140704077653128 \n",
"\n",
"[1, 2] \n",
"ID (extend): 140704077653128\n"
] ]
} }
], ],
"prompt_number": 7 "prompt_number": 6
}, },
{ {
"cell_type": "code", "cell_type": "code",
@ -2979,6 +2985,102 @@
], ],
"prompt_number": 4 "prompt_number": 4
}, },
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<br>\n",
"<br>\n",
"<a name='generator_rhs'>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Only the first clause of generators is evaluated immediately"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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",
"However, the first clause of an generator is already checked upon it's creation, as the following example demonstrates:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"gen_fails = (i for i in 1/0)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"ename": "ZeroDivisionError",
"evalue": "division by zero",
"output_type": "pyerr",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mZeroDivisionError\u001b[0m Traceback (most recent call last)",
"\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",
"\u001b[0;31mZeroDivisionError\u001b[0m: division by zero"
]
}
],
"prompt_number": 18
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"gen_succeeds = (i for i in range(5) for j in 1/0)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 19
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print('But obviously fails when we iterate ...')\n",
"for i in gen_succeeds:\n",
" print(i)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"ename": "ZeroDivisionError",
"evalue": "division by zero",
"output_type": "pyerr",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mZeroDivisionError\u001b[0m Traceback (most recent call last)",
"\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",
"\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",
"\u001b[0;31mZeroDivisionError\u001b[0m: division by zero"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"But obviously fails when we iterate ...\n"
]
}
],
"prompt_number": 20
},
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
@ -3009,19 +3111,10 @@
"metadata": {}, "metadata": {},
"source": [ "source": [
"#### 04/27/2014\n", "#### 04/27/2014\n",
"- minor fixes of typos\n", "- minor fixes of typos \n",
"- single- vs. double-underscore clarification in the private class section." "- new section: \"Only the first clause of generators is evaluated immediately\""
] ]
}, },
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{ {
"cell_type": "code", "cell_type": "code",
"collapsed": false, "collapsed": false,