minor fixes

This commit is contained in:
rasbt 2014-04-26 23:38:56 -04:00
parent 293525b22b
commit d87b987f4c
2 changed files with 72 additions and 8 deletions

View File

@ -1,7 +1,7 @@
{
"metadata": {
"name": "",
"signature": "sha256:3313c0dcca1fddb3782f61571ea24bd2c1afea3d9adfb6c1c65ba959d9ce3c7e"
"signature": "sha256:67969145fdd440ec49d6e6976fa2ea5ffc6db16ed8c1a3f8371040f1c0e97617"
},
"nbformat": 3,
"nbformat_minor": 0,
@ -59,7 +59,7 @@
"source": [
"# Sections\n",
"- [The C3 class resolution algorithm for multiple class inheritance](#c3_class_res)\n",
"- [Using `+=` on lists creates new objects](#pm_in_lists)\n",
"- [Assignment operators and lists - simple-add vs. add-AND operators](#pm_in_lists)\n",
"- [`True` and `False` in the datetime module](#datetime_module)\n",
"- [Python reuses objects for small integers - always use \"==\" for equality, \"is\" for identity](#python_small_int)\n",
"- [Shallow vs. deep copies if list contains other structures and objects](#shallow_vs_deep)\n",
@ -215,7 +215,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## Using `+=` on lists creates new objects"
"## Assignment operators and lists - simple-add vs. add-AND operators"
]
},
{
@ -1010,7 +1010,7 @@
]
}
],
"prompt_number": 29
"prompt_number": 7
},
{
"cell_type": "markdown",
@ -1042,7 +1042,39 @@
]
}
],
"prompt_number": 30
"prompt_number": 9
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"And if you are really keen on using lists, there is a nifty trick that circumvents this problem as a reader nicely pointed out in the comments: We can simply pass the loop variable `i` as a default argument to the lambdas."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"my_list = [lambda x=i: x for i in range(5)]\n",
"for l in my_list:\n",
" print(l())"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"0\n",
"1\n",
"2\n",
"3\n",
"4\n"
]
}
],
"prompt_number": 10
},
{
"cell_type": "markdown",

View File

@ -1,7 +1,7 @@
{
"metadata": {
"name": "",
"signature": "sha256:a53f896c4edbd7706754674c5f4b46daf5a6b5c00670d0627ddac871550f3717"
"signature": "sha256:67969145fdd440ec49d6e6976fa2ea5ffc6db16ed8c1a3f8371040f1c0e97617"
},
"nbformat": 3,
"nbformat_minor": 0,
@ -1010,7 +1010,7 @@
]
}
],
"prompt_number": 29
"prompt_number": 7
},
{
"cell_type": "markdown",
@ -1042,7 +1042,39 @@
]
}
],
"prompt_number": 30
"prompt_number": 9
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"And if you are really keen on using lists, there is a nifty trick that circumvents this problem as a reader nicely pointed out in the comments: We can simply pass the loop variable `i` as a default argument to the lambdas."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"my_list = [lambda x=i: x for i in range(5)]\n",
"for l in my_list:\n",
" print(l())"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"0\n",
"1\n",
"2\n",
"3\n",
"4\n"
]
}
],
"prompt_number": 10
},
{
"cell_type": "markdown",