Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save petermchale/4b6c7f9e1a805c1b54177f84b0787dd8 to your computer and use it in GitHub Desktop.
Save petermchale/4b6c7f9e1a805c1b54177f84b0787dd8 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"id": "92cb4997",
"metadata": {},
"source": [
"## Unexpected behavior when using mutable default parameter values"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "8dc1a928",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"f:\n",
" first call: 1\n",
" second call: 1\n",
"g:\n",
" first call: [1]\n",
" first call: [1, 1]\n"
]
}
],
"source": [
"def f(a=0):\n",
" a += 1\n",
" return a\n",
"\n",
"def g(a=[]):\n",
" a += [1]\n",
" return a\n",
"\n",
"print('f:')\n",
"print(f' first call: {f()}')\n",
"print(f' second call: {f()}')\n",
"\n",
"print('g:')\n",
"print(f' first call: {g()}')\n",
"print(f' first call: {g()}')"
]
},
{
"cell_type": "markdown",
"id": "1450a846",
"metadata": {},
"source": [
"## Explaining the behavior\n",
"A function definition creates a function object. One of the attributes of such an object is the function's default parameter. The default parameter of f is immutable, but the default parameter of g is mutable. Therefore, every change that is made to g's default parameter in it's body in every call changes an attribute of the global function object. \n",
"\n",
"Essentially the following is occurring: "
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "c632d793",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a points to a different object than \"attribute\" because \"0\" is immutable:\n",
" attribute: 0, a: 1\n",
" attribute: 0, a: 1\n",
"a points to same object as \"attribute\" because \"[]\" is mutable:\n",
" attribute: [1], a: [1]\n",
" attribute: [1, 1], a: [1, 1]\n"
]
}
],
"source": [
"attribute = 0\n",
"a = attribute\n",
"a += 1\n",
"print(f'a points to a different object than \"attribute\" because \"0\" is immutable:')\n",
"print(f' attribute: {attribute}, a: {a}')\n",
"a = attribute\n",
"a += 1\n",
"print(f' attribute: {attribute}, a: {a}')\n",
"\n",
"attribute = []\n",
"a = attribute\n",
"a += [1]\n",
"print(f'a points to same object as \"attribute\" because \"[]\" is mutable:')\n",
"print(f' attribute: {attribute}, a: {a}')\n",
"a = attribute\n",
"a += [1]\n",
"print(f' attribute: {attribute}, a: {a}')\n"
]
},
{
"cell_type": "markdown",
"id": "5409c232",
"metadata": {},
"source": [
"## Best practice\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "f4259488",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"good_g:\n",
" first call: [1]\n",
" first call: [1]\n"
]
}
],
"source": [
"def good_g(a=None):\n",
" if a is None:\n",
" a = []\n",
" a += [1]\n",
" return a\n",
"\n",
"print('good_g:')\n",
"print(f' first call: {good_g()}')\n",
"print(f' first call: {good_g()}')"
]
},
{
"cell_type": "markdown",
"id": "a1200c55",
"metadata": {},
"source": [
"## Resources\n",
"\n",
"http://www.omahapython.org/IdiomaticPython.html#default-parameter-values"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "24b876fc",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.7"
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment