mutable objects as default arguments

This commit is contained in:
rasbt 2014-04-15 15:50:39 -04:00
parent 19e5613f57
commit 0bab1bbca1
2 changed files with 15 additions and 10 deletions

View File

@ -1,7 +1,7 @@
{
"metadata": {
"name": "",
"signature": "sha256:6bc56360203df31596f6332e4b5c73effb2572d18b60bdf6a792042e2fde8c12"
"signature": "sha256:0c860c383edcd7622b0c660eb989513e90496c7350916bae1ddb6c10f29fab2c"
},
"nbformat": 3,
"nbformat_minor": 0,
@ -40,9 +40,10 @@
"- [The C3 class resolution algorithm for multiple class inheritance](#c3_class_res)\n",
"- [The behavior of += for lists](#pm_in_lists)\n",
"- [`True` and `False` in the datetime module](#datetime_module)\n",
"- [Python reuses objects for small integers](#python_small_int)\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",
"- [Picking True values from and and or expressions](#false_true_expressions)"
"- [Picking True values from and and or expressions](#false_true_expressions)\n",
"- [Don't use mutable objects as default arguments for functions!](#def_mutable_func)"
]
},
{
@ -189,10 +190,12 @@
"<br>\n",
"<br>\n",
"<a name='python_small_int'></a>\n",
"## Python reuses objects for small integers \n",
"## Python reuses objects for small integers - always use \"==\" for equality, \"is\" for identity\n",
"\n",
"This oddity occurs, because Python tends to stores small integers as the same object, but not so for larger ones! \n",
"(*I received a comment that this is in fact a CPython artefact and must not necessarily be true in all implementations of Python!*)"
"(*I received a comment that this is in fact a CPython artefact and must not necessarily be true in all implementations of Python!*)\n",
"\n",
"So the take home message is: always use \"==\" for equality, \"is\" for identity!"
]
},
{
@ -335,7 +338,7 @@
"source": [
"<br>\n",
"<br>\n",
"<a name='def_mutable_func></a>\n",
"<a name='def_mutable_func'></a>\n",
"## Don't use mutable objects as default arguments for functions!"
]
},

View File

@ -1,7 +1,7 @@
{
"metadata": {
"name": "",
"signature": "sha256:158ad35cd56abe8905983dc63421b3b7b54d28a947df54f67a3b7f0a53e31481"
"signature": "sha256:0c860c383edcd7622b0c660eb989513e90496c7350916bae1ddb6c10f29fab2c"
},
"nbformat": 3,
"nbformat_minor": 0,
@ -40,7 +40,7 @@
"- [The C3 class resolution algorithm for multiple class inheritance](#c3_class_res)\n",
"- [The behavior of += for lists](#pm_in_lists)\n",
"- [`True` and `False` in the datetime module](#datetime_module)\n",
"- [Python reuses objects for small integers](#python_small_int)\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",
"- [Picking True values from and and or expressions](#false_true_expressions)\n",
"- [Don't use mutable objects as default arguments for functions!](#def_mutable_func)"
@ -190,10 +190,12 @@
"<br>\n",
"<br>\n",
"<a name='python_small_int'></a>\n",
"## Python reuses objects for small integers \n",
"## Python reuses objects for small integers - always use \"==\" for equality, \"is\" for identity\n",
"\n",
"This oddity occurs, because Python tends to stores small integers as the same object, but not so for larger ones! \n",
"(*I received a comment that this is in fact a CPython artefact and must not necessarily be true in all implementations of Python!*)"
"(*I received a comment that this is in fact a CPython artefact and must not necessarily be true in all implementations of Python!*)\n",
"\n",
"So the take home message is: always use \"==\" for equality, \"is\" for identity!"
]
},
{