mutable objects as default arguments

This commit is contained in:
rasbt 2014-04-15 16:37:08 -04:00
parent 0bab1bbca1
commit a305b990c4
2 changed files with 118 additions and 2 deletions

View File

@ -1,7 +1,7 @@
{
"metadata": {
"name": "",
"signature": "sha256:0c860c383edcd7622b0c660eb989513e90496c7350916bae1ddb6c10f29fab2c"
"signature": "sha256:611615765638a390c8e9093179758c4a23524ddcc2379aeff80e51c22031aa18"
},
"nbformat": 3,
"nbformat_minor": 0,
@ -225,6 +225,64 @@
],
"prompt_number": 5
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Another popular example to illustrate the reuse of objects for small integers is:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print(256 is 257 - 1)\n",
"print(257 is 258 - 1)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"True\n",
"False\n"
]
}
],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"And to illustrate the test for equality (`==`) vs. identity (`is`):"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"a = 'hello world!'\n",
"b = 'hello world!'\n",
"print('a is b', a is b)\n",
"print('a == b', a == b)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"a is b False\n",
"a == b True\n"
]
}
],
"prompt_number": 3
},
{
"cell_type": "markdown",
"metadata": {},

View File

@ -1,7 +1,7 @@
{
"metadata": {
"name": "",
"signature": "sha256:0c860c383edcd7622b0c660eb989513e90496c7350916bae1ddb6c10f29fab2c"
"signature": "sha256:611615765638a390c8e9093179758c4a23524ddcc2379aeff80e51c22031aa18"
},
"nbformat": 3,
"nbformat_minor": 0,
@ -225,6 +225,64 @@
],
"prompt_number": 5
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Another popular example to illustrate the reuse of objects for small integers is:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print(256 is 257 - 1)\n",
"print(257 is 258 - 1)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"True\n",
"False\n"
]
}
],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"And to illustrate the test for equality (`==`) vs. identity (`is`):"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"a = 'hello world!'\n",
"b = 'hello world!'\n",
"print('a is b', a is b)\n",
"print('a == b', a == b)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"a is b False\n",
"a == b True\n"
]
}
],
"prompt_number": 3
},
{
"cell_type": "markdown",
"metadata": {},