Skip to content

Commit d87b987

Browse files
committed
minor fixes
1 parent 293525b commit d87b987

2 files changed

Lines changed: 72 additions & 8 deletions

File tree

.ipynb_checkpoints/not_so_obvious_python_stuff-checkpoint.ipynb

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"metadata": {
33
"name": "",
4-
"signature": "sha256:3313c0dcca1fddb3782f61571ea24bd2c1afea3d9adfb6c1c65ba959d9ce3c7e"
4+
"signature": "sha256:67969145fdd440ec49d6e6976fa2ea5ffc6db16ed8c1a3f8371040f1c0e97617"
55
},
66
"nbformat": 3,
77
"nbformat_minor": 0,
@@ -59,7 +59,7 @@
5959
"source": [
6060
"# Sections\n",
6161
"- [The C3 class resolution algorithm for multiple class inheritance](#c3_class_res)\n",
62-
"- [Using `+=` on lists creates new objects](#pm_in_lists)\n",
62+
"- [Assignment operators and lists - simple-add vs. add-AND operators](#pm_in_lists)\n",
6363
"- [`True` and `False` in the datetime module](#datetime_module)\n",
6464
"- [Python reuses objects for small integers - always use \"==\" for equality, \"is\" for identity](#python_small_int)\n",
6565
"- [Shallow vs. deep copies if list contains other structures and objects](#shallow_vs_deep)\n",
@@ -215,7 +215,7 @@
215215
"cell_type": "markdown",
216216
"metadata": {},
217217
"source": [
218-
"## Using `+=` on lists creates new objects"
218+
"## Assignment operators and lists - simple-add vs. add-AND operators"
219219
]
220220
},
221221
{
@@ -1010,7 +1010,7 @@
10101010
]
10111011
}
10121012
],
1013-
"prompt_number": 29
1013+
"prompt_number": 7
10141014
},
10151015
{
10161016
"cell_type": "markdown",
@@ -1042,7 +1042,39 @@
10421042
]
10431043
}
10441044
],
1045-
"prompt_number": 30
1045+
"prompt_number": 9
1046+
},
1047+
{
1048+
"cell_type": "markdown",
1049+
"metadata": {},
1050+
"source": [
1051+
"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."
1052+
]
1053+
},
1054+
{
1055+
"cell_type": "code",
1056+
"collapsed": false,
1057+
"input": [
1058+
"my_list = [lambda x=i: x for i in range(5)]\n",
1059+
"for l in my_list:\n",
1060+
" print(l())"
1061+
],
1062+
"language": "python",
1063+
"metadata": {},
1064+
"outputs": [
1065+
{
1066+
"output_type": "stream",
1067+
"stream": "stdout",
1068+
"text": [
1069+
"0\n",
1070+
"1\n",
1071+
"2\n",
1072+
"3\n",
1073+
"4\n"
1074+
]
1075+
}
1076+
],
1077+
"prompt_number": 10
10461078
},
10471079
{
10481080
"cell_type": "markdown",

not_so_obvious_python_stuff.ipynb

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"metadata": {
33
"name": "",
4-
"signature": "sha256:a53f896c4edbd7706754674c5f4b46daf5a6b5c00670d0627ddac871550f3717"
4+
"signature": "sha256:67969145fdd440ec49d6e6976fa2ea5ffc6db16ed8c1a3f8371040f1c0e97617"
55
},
66
"nbformat": 3,
77
"nbformat_minor": 0,
@@ -1010,7 +1010,7 @@
10101010
]
10111011
}
10121012
],
1013-
"prompt_number": 29
1013+
"prompt_number": 7
10141014
},
10151015
{
10161016
"cell_type": "markdown",
@@ -1042,7 +1042,39 @@
10421042
]
10431043
}
10441044
],
1045-
"prompt_number": 30
1045+
"prompt_number": 9
1046+
},
1047+
{
1048+
"cell_type": "markdown",
1049+
"metadata": {},
1050+
"source": [
1051+
"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."
1052+
]
1053+
},
1054+
{
1055+
"cell_type": "code",
1056+
"collapsed": false,
1057+
"input": [
1058+
"my_list = [lambda x=i: x for i in range(5)]\n",
1059+
"for l in my_list:\n",
1060+
" print(l())"
1061+
],
1062+
"language": "python",
1063+
"metadata": {},
1064+
"outputs": [
1065+
{
1066+
"output_type": "stream",
1067+
"stream": "stdout",
1068+
"text": [
1069+
"0\n",
1070+
"1\n",
1071+
"2\n",
1072+
"3\n",
1073+
"4\n"
1074+
]
1075+
}
1076+
],
1077+
"prompt_number": 10
10461078
},
10471079
{
10481080
"cell_type": "markdown",

0 commit comments

Comments
 (0)