mirror of
https://github.com/rasbt/python_reference.git
synced 2025-02-22 08:12:20 +00:00
mutable objects as default arguments
This commit is contained in:
parent
6c95918f3d
commit
ed255a9ad7
|
@ -1,7 +1,7 @@
|
||||||
{
|
{
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"name": "",
|
"name": "",
|
||||||
"signature": "sha256:ed543f19540359765672e007e235b9a4f6e76963cacf74e34dac5612ea7d8b94"
|
"signature": "sha256:6bc56360203df31596f6332e4b5c73effb2572d18b60bdf6a792042e2fde8c12"
|
||||||
},
|
},
|
||||||
"nbformat": 3,
|
"nbformat": 3,
|
||||||
"nbformat_minor": 0,
|
"nbformat_minor": 0,
|
||||||
|
@ -328,6 +328,61 @@
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"prompt_number": 9
|
"prompt_number": 9
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"<br>\n",
|
||||||
|
"<br>\n",
|
||||||
|
"<a name='def_mutable_func></a>\n",
|
||||||
|
"## Don't use mutable objects as default arguments for functions!"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"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",
|
||||||
|
"\n",
|
||||||
|
"(Original source: [http://docs.python-guide.org/en/latest/writing/gotchas/](http://docs.python-guide.org/en/latest/writing/gotchas/)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"collapsed": false,
|
||||||
|
"input": [
|
||||||
|
"def append_to_list(value, def_list=[]):\n",
|
||||||
|
" def_list.append(value)\n",
|
||||||
|
" return def_list\n",
|
||||||
|
"\n",
|
||||||
|
"my_list = append_to_list(1)\n",
|
||||||
|
"print(my_list)\n",
|
||||||
|
"\n",
|
||||||
|
"my_other_list = append_to_list(2)\n",
|
||||||
|
"print(my_other_list)"
|
||||||
|
],
|
||||||
|
"language": "python",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"output_type": "stream",
|
||||||
|
"stream": "stdout",
|
||||||
|
"text": [
|
||||||
|
"[1]\n",
|
||||||
|
"[1, 2]\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"prompt_number": 1
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"collapsed": false,
|
||||||
|
"input": [],
|
||||||
|
"language": "python",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": []
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"metadata": {}
|
"metadata": {}
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
{
|
{
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"name": "",
|
"name": "",
|
||||||
"signature": "sha256:ed543f19540359765672e007e235b9a4f6e76963cacf74e34dac5612ea7d8b94"
|
"signature": "sha256:6bc56360203df31596f6332e4b5c73effb2572d18b60bdf6a792042e2fde8c12"
|
||||||
},
|
},
|
||||||
"nbformat": 3,
|
"nbformat": 3,
|
||||||
"nbformat_minor": 0,
|
"nbformat_minor": 0,
|
||||||
|
@ -328,6 +328,61 @@
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"prompt_number": 9
|
"prompt_number": 9
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"<br>\n",
|
||||||
|
"<br>\n",
|
||||||
|
"<a name='def_mutable_func></a>\n",
|
||||||
|
"## Don't use mutable objects as default arguments for functions!"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"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",
|
||||||
|
"\n",
|
||||||
|
"(Original source: [http://docs.python-guide.org/en/latest/writing/gotchas/](http://docs.python-guide.org/en/latest/writing/gotchas/)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"collapsed": false,
|
||||||
|
"input": [
|
||||||
|
"def append_to_list(value, def_list=[]):\n",
|
||||||
|
" def_list.append(value)\n",
|
||||||
|
" return def_list\n",
|
||||||
|
"\n",
|
||||||
|
"my_list = append_to_list(1)\n",
|
||||||
|
"print(my_list)\n",
|
||||||
|
"\n",
|
||||||
|
"my_other_list = append_to_list(2)\n",
|
||||||
|
"print(my_other_list)"
|
||||||
|
],
|
||||||
|
"language": "python",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"output_type": "stream",
|
||||||
|
"stream": "stdout",
|
||||||
|
"text": [
|
||||||
|
"[1]\n",
|
||||||
|
"[1, 2]\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"prompt_number": 1
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"collapsed": false,
|
||||||
|
"input": [],
|
||||||
|
"language": "python",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": []
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"metadata": {}
|
"metadata": {}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user