Skip to content

Commit f8ebfc8

Browse files
committed
mutable objects as default arguments
1 parent be6a8db commit f8ebfc8

2 files changed

Lines changed: 64 additions & 35 deletions

File tree

.ipynb_checkpoints/not_so_obvious_python_stuff-checkpoint.ipynb

Lines changed: 58 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"metadata": {
33
"name": "",
4-
"signature": "sha256:5c0e4a56352cc8a60aa6e2ced2611bc186078af3c849647256f0c4a810004ee9"
4+
"signature": "sha256:b6e7356842906c447ef3c7520aac098e7fe4f888e46fdd5ae06905f1ebe68e27"
55
},
66
"nbformat": 3,
77
"nbformat_minor": 0,
@@ -48,7 +48,8 @@
4848
"- [`bool` is a subclass of `int`](#bool_int)\n",
4949
"- [About lambda and closures-in-a-loop pitfall](#lambda_closure)\n",
5050
"- [Python's LEGB scope resolution and the keywords `global` and `nonlocal`](#python_legb)\n",
51-
"- [When immutable Tuples aren't so immutable](#immutable_tuple)"
51+
"- [When immutable Tuples aren't so immutable](#immutable_tuple)\n",
52+
"- [List comprehensions are fast, but generators are faster!?](#list_generator)"
5253
]
5354
},
5455
{
@@ -966,7 +967,15 @@
966967
"cell_type": "markdown",
967968
"metadata": {},
968969
"source": [
969-
"## List comprehensions are fast, but generators are faster!\n"
970+
"<br>\n",
971+
"<br>\n",
972+
"<a name='list_generator'></a>\n",
973+
"\n",
974+
"## List comprehensions are fast, but generators are faster!?\n",
975+
"\n",
976+
"Not, really (or significantly, see the benchmarks below). So what's the reason to prefer one over the other?\n",
977+
"- use lists if you want to use list methods \n",
978+
"- use generators when you are dealing with huge collections to avoid memory issues"
970979
]
971980
},
972981
{
@@ -975,30 +984,30 @@
975984
"input": [
976985
"import timeit\n",
977986
"\n",
978-
"def plainlist():\n",
987+
"def plainlist(n=100000):\n",
979988
" my_list = []\n",
980-
" for i in range(1,1000000):\n",
989+
" for i in range(n):\n",
981990
" if i % 5 == 0:\n",
982991
" my_list.append(i)\n",
983992
" return my_list\n",
984993
"\n",
985-
"def listcompr():\n",
986-
" my_list = [i for i in range(1,1000000) if i % 5 == 0]\n",
994+
"def listcompr(n=100000):\n",
995+
" my_list = [i for i in range(n) if i % 5 == 0]\n",
987996
" return my_list\n",
988997
"\n",
989-
"def generator():\n",
990-
" my_gen = (i for i in range(1,1000000) if i % 5 == 0)\n",
998+
"def generator(n=100000):\n",
999+
" my_gen = (i for i in range(n) if i % 5 == 0)\n",
9911000
" return my_gen\n",
9921001
"\n",
993-
"def generator_yield():\n",
994-
" for i in range(1,1000000):\n",
1002+
"def generator_yield(n=100000):\n",
1003+
" for i in range(n):\n",
9951004
" if i % 5 == 0:\n",
9961005
" yield i"
9971006
],
9981007
"language": "python",
9991008
"metadata": {},
10001009
"outputs": [],
1001-
"prompt_number": 60
1010+
"prompt_number": 75
10021011
},
10031012
{
10041013
"cell_type": "markdown",
@@ -1027,10 +1036,13 @@
10271036
" for i in listcompr:\n",
10281037
" print(i)\n",
10291038
"\n",
1030-
"print('plain_list', end = '')\n",
1039+
"print('plain_list: ', end = '')\n",
10311040
"%timeit test_plainlist\n",
1041+
"print('\\nlistcompr: ', end = '')\n",
10321042
"%timeit test_listcompr\n",
1043+
"print('\\ngenerator: ', end = '')\n",
10331044
"%timeit test_generator\n",
1045+
"print('\\ngenerator_yield: ', end = '')\n",
10341046
"%timeit test_generator_yield"
10351047
],
10361048
"language": "python",
@@ -1040,28 +1052,45 @@
10401052
"output_type": "stream",
10411053
"stream": "stdout",
10421054
"text": [
1043-
"plain_list"
1055+
"plain_list: 10000000 loops, best of 3: 26.2 ns per loop"
10441056
]
10451057
},
10461058
{
1047-
"ename": "NameError",
1048-
"evalue": "global name 'test_plainlist' is not defined",
1049-
"output_type": "pyerr",
1050-
"traceback": [
1051-
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
1052-
"\u001b[0;32m<ipython-input-62-a2bedd84850f>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 16\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 17\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'plain_list'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mend\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m''\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 18\u001b[0;31m \u001b[0mget_ipython\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmagic\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'timeit test_plainlist'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 19\u001b[0m \u001b[0mget_ipython\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmagic\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'timeit test_listcompr'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 20\u001b[0m \u001b[0mget_ipython\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmagic\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'timeit test_generator'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
1053-
"\u001b[0;32m/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/IPython/core/interactiveshell.py\u001b[0m in \u001b[0;36mmagic\u001b[0;34m(self, arg_s)\u001b[0m\n\u001b[1;32m 2203\u001b[0m \u001b[0mmagic_name\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0m_\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmagic_arg_s\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0marg_s\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpartition\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m' '\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2204\u001b[0m \u001b[0mmagic_name\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmagic_name\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlstrip\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mprefilter\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mESC_MAGIC\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 2205\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrun_line_magic\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmagic_name\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmagic_arg_s\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2206\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2207\u001b[0m \u001b[0;31m#-------------------------------------------------------------------------\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
1054-
"\u001b[0;32m/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/IPython/core/interactiveshell.py\u001b[0m in \u001b[0;36mrun_line_magic\u001b[0;34m(self, magic_name, line)\u001b[0m\n\u001b[1;32m 2124\u001b[0m \u001b[0mkwargs\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'local_ns'\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0msys\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_getframe\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mstack_depth\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mf_locals\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2125\u001b[0m \u001b[0;32mwith\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mbuiltin_trap\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 2126\u001b[0;31m \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mfn\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2127\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mresult\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2128\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
1055-
"\u001b[0;32m/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/IPython/core/magics/execution.py\u001b[0m in \u001b[0;36mtimeit\u001b[0;34m(self, line, cell)\u001b[0m\n",
1056-
"\u001b[0;32m/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/IPython/core/magic.py\u001b[0m in \u001b[0;36m<lambda>\u001b[0;34m(f, *a, **k)\u001b[0m\n\u001b[1;32m 191\u001b[0m \u001b[0;31m# but it's overkill for just that one bit of state.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 192\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mmagic_deco\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0marg\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 193\u001b[0;31m \u001b[0mcall\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;32mlambda\u001b[0m \u001b[0mf\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mk\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mf\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mk\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 194\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 195\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mcallable\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0marg\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
1057-
"\u001b[0;32m/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/IPython/core/magics/execution.py\u001b[0m in \u001b[0;36mtimeit\u001b[0;34m(self, line, cell)\u001b[0m\n\u001b[1;32m 1011\u001b[0m \u001b[0mnumber\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1012\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0m_\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m10\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1013\u001b[0;31m \u001b[0;32mif\u001b[0m \u001b[0mtimer\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtimeit\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnumber\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m>=\u001b[0m \u001b[0;36m0.2\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1014\u001b[0m \u001b[0;32mbreak\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1015\u001b[0m \u001b[0mnumber\u001b[0m \u001b[0;34m*=\u001b[0m \u001b[0;36m10\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
1058-
"\u001b[0;32m/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/timeit.py\u001b[0m in \u001b[0;36mtimeit\u001b[0;34m(self, number)\u001b[0m\n\u001b[1;32m 188\u001b[0m \u001b[0mgc\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdisable\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 189\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 190\u001b[0;31m \u001b[0mtiming\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0minner\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mit\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtimer\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 191\u001b[0m \u001b[0;32mfinally\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 192\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mgcold\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
1059-
"\u001b[0;32m<magic-timeit>\u001b[0m in \u001b[0;36minner\u001b[0;34m(_it, _timer)\u001b[0m\n",
1060-
"\u001b[0;31mNameError\u001b[0m: global name 'test_plainlist' is not defined"
1059+
"output_type": "stream",
1060+
"stream": "stdout",
1061+
"text": [
1062+
"\n",
1063+
"\n",
1064+
"listcompr: 10000000 loops, best of 3: 26.1 ns per loop"
1065+
]
1066+
},
1067+
{
1068+
"output_type": "stream",
1069+
"stream": "stdout",
1070+
"text": [
1071+
"\n",
1072+
"\n",
1073+
"generator: 10000000 loops, best of 3: 25.9 ns per loop"
1074+
]
1075+
},
1076+
{
1077+
"output_type": "stream",
1078+
"stream": "stdout",
1079+
"text": [
1080+
"\n",
1081+
"\n",
1082+
"generator_yield: 10000000 loops, best of 3: 26 ns per loop"
1083+
]
1084+
},
1085+
{
1086+
"output_type": "stream",
1087+
"stream": "stdout",
1088+
"text": [
1089+
"\n"
10611090
]
10621091
}
10631092
],
1064-
"prompt_number": 62
1093+
"prompt_number": 76
10651094
},
10661095
{
10671096
"cell_type": "code",

not_so_obvious_python_stuff.ipynb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"metadata": {
33
"name": "",
4-
"signature": "sha256:a2c062243bccabbfe46e6f62bbc9d6dd5b10fe44d091727781108ba5b87b9e28"
4+
"signature": "sha256:b6e7356842906c447ef3c7520aac098e7fe4f888e46fdd5ae06905f1ebe68e27"
55
},
66
"nbformat": 3,
77
"nbformat_minor": 0,
@@ -48,7 +48,8 @@
4848
"- [`bool` is a subclass of `int`](#bool_int)\n",
4949
"- [About lambda and closures-in-a-loop pitfall](#lambda_closure)\n",
5050
"- [Python's LEGB scope resolution and the keywords `global` and `nonlocal`](#python_legb)\n",
51-
"- [When immutable Tuples aren't so immutable](#immutable_tuple)"
51+
"- [When immutable Tuples aren't so immutable](#immutable_tuple)\n",
52+
"- [List comprehensions are fast, but generators are faster!?](#list_generator)"
5253
]
5354
},
5455
{
@@ -967,9 +968,10 @@
967968
"metadata": {},
968969
"source": [
969970
"<br>\n",
971+
"<br>\n",
972+
"<a name='list_generator'></a>\n",
970973
"\n",
971-
"\n",
972-
"## List comprehensions are fast, but generators are faster!\n",
974+
"## List comprehensions are fast, but generators are faster!?\n",
973975
"\n",
974976
"Not, really (or significantly, see the benchmarks below). So what's the reason to prefer one over the other?\n",
975977
"- use lists if you want to use list methods \n",
@@ -1018,8 +1020,6 @@
10181020
"cell_type": "code",
10191021
"collapsed": false,
10201022
"input": [
1021-
"\n",
1022-
"\n",
10231023
"def test_plainlist(plain_list):\n",
10241024
" for i in plain_list:\n",
10251025
" print(i)\n",

0 commit comments

Comments
 (0)