mutable objects as default arguments

This commit is contained in:
rasbt 2014-04-15 16:40:35 -04:00
parent a305b990c4
commit 9483e549c3
2 changed files with 74 additions and 16 deletions

View File

@ -1,7 +1,7 @@
{ {
"metadata": { "metadata": {
"name": "", "name": "",
"signature": "sha256:611615765638a390c8e9093179758c4a23524ddcc2379aeff80e51c22031aa18" "signature": "sha256:913662e78e4e0a2e83e289d74c42e197b1d5af6c2f589057de821734b0141c07"
}, },
"nbformat": 3, "nbformat": 3,
"nbformat_minor": 0, "nbformat_minor": 0,
@ -229,7 +229,7 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"Another popular example to illustrate the reuse of objects for small integers is:" "#### Another popular example to illustrate the reuse of objects for small integers is:"
] ]
}, },
{ {
@ -257,7 +257,7 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"And to illustrate the test for equality (`==`) vs. identity (`is`):" "#### And to illustrate the test for equality (`==`) vs. identity (`is`):"
] ]
}, },
{ {
@ -266,8 +266,8 @@
"input": [ "input": [
"a = 'hello world!'\n", "a = 'hello world!'\n",
"b = 'hello world!'\n", "b = 'hello world!'\n",
"print('a is b', a is b)\n", "print('a is b,', a is b)\n",
"print('a == b', a == b)" "print('a == b,', a == b)"
], ],
"language": "python", "language": "python",
"metadata": {}, "metadata": {},
@ -276,12 +276,41 @@
"output_type": "stream", "output_type": "stream",
"stream": "stdout", "stream": "stdout",
"text": [ "text": [
"a is b False\n", "a is b, False\n",
"a == b True\n" "a == b, True\n"
] ]
} }
], ],
"prompt_number": 3 "prompt_number": 6
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### And this example shows when `==` does not necessarilu implies that two objects are the same:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"a = float('nan')\n",
"print('a == a,', a == a)\n",
"print('a is a,', a is a)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"a == a, False\n",
"a is a, True\n"
]
}
],
"prompt_number": 7
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",

View File

@ -1,7 +1,7 @@
{ {
"metadata": { "metadata": {
"name": "", "name": "",
"signature": "sha256:611615765638a390c8e9093179758c4a23524ddcc2379aeff80e51c22031aa18" "signature": "sha256:913662e78e4e0a2e83e289d74c42e197b1d5af6c2f589057de821734b0141c07"
}, },
"nbformat": 3, "nbformat": 3,
"nbformat_minor": 0, "nbformat_minor": 0,
@ -229,7 +229,7 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"Another popular example to illustrate the reuse of objects for small integers is:" "#### Another popular example to illustrate the reuse of objects for small integers is:"
] ]
}, },
{ {
@ -257,7 +257,7 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"And to illustrate the test for equality (`==`) vs. identity (`is`):" "#### And to illustrate the test for equality (`==`) vs. identity (`is`):"
] ]
}, },
{ {
@ -266,8 +266,8 @@
"input": [ "input": [
"a = 'hello world!'\n", "a = 'hello world!'\n",
"b = 'hello world!'\n", "b = 'hello world!'\n",
"print('a is b', a is b)\n", "print('a is b,', a is b)\n",
"print('a == b', a == b)" "print('a == b,', a == b)"
], ],
"language": "python", "language": "python",
"metadata": {}, "metadata": {},
@ -276,12 +276,41 @@
"output_type": "stream", "output_type": "stream",
"stream": "stdout", "stream": "stdout",
"text": [ "text": [
"a is b False\n", "a is b, False\n",
"a == b True\n" "a == b, True\n"
] ]
} }
], ],
"prompt_number": 3 "prompt_number": 6
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### And this example shows when `==` does not necessarilu implies that two objects are the same:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"a = float('nan')\n",
"print('a == a,', a == a)\n",
"print('a is a,', a is a)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"a == a, False\n",
"a is a, True\n"
]
}
],
"prompt_number": 7
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",