Skip to content

Commit ed255a9

Browse files
committed
mutable objects as default arguments
1 parent 6c95918 commit ed255a9

2 files changed

Lines changed: 112 additions & 2 deletions

File tree

.ipynb_checkpoints/not_so_obvious_python_stuff-checkpoint.ipynb

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"metadata": {
33
"name": "",
4-
"signature": "sha256:ed543f19540359765672e007e235b9a4f6e76963cacf74e34dac5612ea7d8b94"
4+
"signature": "sha256:6bc56360203df31596f6332e4b5c73effb2572d18b60bdf6a792042e2fde8c12"
55
},
66
"nbformat": 3,
77
"nbformat_minor": 0,
@@ -328,6 +328,61 @@
328328
}
329329
],
330330
"prompt_number": 9
331+
},
332+
{
333+
"cell_type": "markdown",
334+
"metadata": {},
335+
"source": [
336+
"<br>\n",
337+
"<br>\n",
338+
"<a name='def_mutable_func></a>\n",
339+
"## Don't use mutable objects as default arguments for functions!"
340+
]
341+
},
342+
{
343+
"cell_type": "markdown",
344+
"metadata": {},
345+
"source": [
346+
"Don't use mutable objects (e.g., dictionaries, lists, sets, etc.) as default arguments for functions! You might expect that a new list is created every time when we call the function without providing an argument for the default parameter, but this is not the case: Python will create the mutable object (default parameter) only the first time the function is called, see the following code:\n",
347+
"\n",
348+
"(Original source: [http://docs.python-guide.org/en/latest/writing/gotchas/](http://docs.python-guide.org/en/latest/writing/gotchas/)"
349+
]
350+
},
351+
{
352+
"cell_type": "code",
353+
"collapsed": false,
354+
"input": [
355+
"def append_to_list(value, def_list=[]):\n",
356+
" def_list.append(value)\n",
357+
" return def_list\n",
358+
"\n",
359+
"my_list = append_to_list(1)\n",
360+
"print(my_list)\n",
361+
"\n",
362+
"my_other_list = append_to_list(2)\n",
363+
"print(my_other_list)"
364+
],
365+
"language": "python",
366+
"metadata": {},
367+
"outputs": [
368+
{
369+
"output_type": "stream",
370+
"stream": "stdout",
371+
"text": [
372+
"[1]\n",
373+
"[1, 2]\n"
374+
]
375+
}
376+
],
377+
"prompt_number": 1
378+
},
379+
{
380+
"cell_type": "code",
381+
"collapsed": false,
382+
"input": [],
383+
"language": "python",
384+
"metadata": {},
385+
"outputs": []
331386
}
332387
],
333388
"metadata": {}

not_so_obvious_python_stuff.ipynb

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"metadata": {
33
"name": "",
4-
"signature": "sha256:ed543f19540359765672e007e235b9a4f6e76963cacf74e34dac5612ea7d8b94"
4+
"signature": "sha256:6bc56360203df31596f6332e4b5c73effb2572d18b60bdf6a792042e2fde8c12"
55
},
66
"nbformat": 3,
77
"nbformat_minor": 0,
@@ -328,6 +328,61 @@
328328
}
329329
],
330330
"prompt_number": 9
331+
},
332+
{
333+
"cell_type": "markdown",
334+
"metadata": {},
335+
"source": [
336+
"<br>\n",
337+
"<br>\n",
338+
"<a name='def_mutable_func></a>\n",
339+
"## Don't use mutable objects as default arguments for functions!"
340+
]
341+
},
342+
{
343+
"cell_type": "markdown",
344+
"metadata": {},
345+
"source": [
346+
"Don't use mutable objects (e.g., dictionaries, lists, sets, etc.) as default arguments for functions! You might expect that a new list is created every time when we call the function without providing an argument for the default parameter, but this is not the case: Python will create the mutable object (default parameter) only the first time the function is called, see the following code:\n",
347+
"\n",
348+
"(Original source: [http://docs.python-guide.org/en/latest/writing/gotchas/](http://docs.python-guide.org/en/latest/writing/gotchas/)"
349+
]
350+
},
351+
{
352+
"cell_type": "code",
353+
"collapsed": false,
354+
"input": [
355+
"def append_to_list(value, def_list=[]):\n",
356+
" def_list.append(value)\n",
357+
" return def_list\n",
358+
"\n",
359+
"my_list = append_to_list(1)\n",
360+
"print(my_list)\n",
361+
"\n",
362+
"my_other_list = append_to_list(2)\n",
363+
"print(my_other_list)"
364+
],
365+
"language": "python",
366+
"metadata": {},
367+
"outputs": [
368+
{
369+
"output_type": "stream",
370+
"stream": "stdout",
371+
"text": [
372+
"[1]\n",
373+
"[1, 2]\n"
374+
]
375+
}
376+
],
377+
"prompt_number": 1
378+
},
379+
{
380+
"cell_type": "code",
381+
"collapsed": false,
382+
"input": [],
383+
"language": "python",
384+
"metadata": {},
385+
"outputs": []
331386
}
332387
],
333388
"metadata": {}

0 commit comments

Comments
 (0)