From 9483e549c31bbd3fb61fdd311188ca5fc4147bb1 Mon Sep 17 00:00:00 2001 From: rasbt Date: Tue, 15 Apr 2014 16:40:35 -0400 Subject: [PATCH] mutable objects as default arguments --- ...t_so_obvious_python_stuff-checkpoint.ipynb | 45 +++++++++++++++---- not_so_obvious_python_stuff.ipynb | 45 +++++++++++++++---- 2 files changed, 74 insertions(+), 16 deletions(-) diff --git a/.ipynb_checkpoints/not_so_obvious_python_stuff-checkpoint.ipynb b/.ipynb_checkpoints/not_so_obvious_python_stuff-checkpoint.ipynb index 0b2dd7c..b412956 100644 --- a/.ipynb_checkpoints/not_so_obvious_python_stuff-checkpoint.ipynb +++ b/.ipynb_checkpoints/not_so_obvious_python_stuff-checkpoint.ipynb @@ -1,7 +1,7 @@ { "metadata": { "name": "", - "signature": "sha256:611615765638a390c8e9093179758c4a23524ddcc2379aeff80e51c22031aa18" + "signature": "sha256:913662e78e4e0a2e83e289d74c42e197b1d5af6c2f589057de821734b0141c07" }, "nbformat": 3, "nbformat_minor": 0, @@ -229,7 +229,7 @@ "cell_type": "markdown", "metadata": {}, "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", "metadata": {}, "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": [ "a = 'hello world!'\n", "b = 'hello world!'\n", - "print('a is b', a is b)\n", - "print('a == b', a == b)" + "print('a is b,', a is b)\n", + "print('a == b,', a == b)" ], "language": "python", "metadata": {}, @@ -276,12 +276,41 @@ "output_type": "stream", "stream": "stdout", "text": [ - "a is b False\n", - "a == b True\n" + "a is b, False\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", diff --git a/not_so_obvious_python_stuff.ipynb b/not_so_obvious_python_stuff.ipynb index 0b2dd7c..b412956 100644 --- a/not_so_obvious_python_stuff.ipynb +++ b/not_so_obvious_python_stuff.ipynb @@ -1,7 +1,7 @@ { "metadata": { "name": "", - "signature": "sha256:611615765638a390c8e9093179758c4a23524ddcc2379aeff80e51c22031aa18" + "signature": "sha256:913662e78e4e0a2e83e289d74c42e197b1d5af6c2f589057de821734b0141c07" }, "nbformat": 3, "nbformat_minor": 0, @@ -229,7 +229,7 @@ "cell_type": "markdown", "metadata": {}, "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", "metadata": {}, "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": [ "a = 'hello world!'\n", "b = 'hello world!'\n", - "print('a is b', a is b)\n", - "print('a == b', a == b)" + "print('a is b,', a is b)\n", + "print('a == b,', a == b)" ], "language": "python", "metadata": {}, @@ -276,12 +276,41 @@ "output_type": "stream", "stream": "stdout", "text": [ - "a is b False\n", - "a == b True\n" + "a is b, False\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",