From 5d040cc0768e7df52921e7acabcedb6fd916994b Mon Sep 17 00:00:00 2001 From: rasbt Date: Wed, 16 Apr 2014 12:45:52 -0400 Subject: [PATCH] small int clarification --- .../not_so_obvious_python_stuff-checkpoint.ipynb | 6 ++++-- not_so_obvious_python_stuff.ipynb | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/.ipynb_checkpoints/not_so_obvious_python_stuff-checkpoint.ipynb b/.ipynb_checkpoints/not_so_obvious_python_stuff-checkpoint.ipynb index 976101b..aea454a 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:161acdaa5124ca9e1082a3b0564e8792f64364ea91cd2cff096fdbbff37408ca" + "signature": "sha256:e20426bb66111696c7ec4f8d754946061e129b3e332a6cc2fd00331b9540d8c5" }, "nbformat": 3, "nbformat_minor": 0, @@ -212,7 +212,9 @@ "\n", "## Python reuses objects for small integers - always use \"==\" for equality, \"is\" for identity\n", "\n", - "This oddity occurs, because Python tends to stores small integers as the same object, but not so for larger ones! \n", + "This oddity occurs, because Python keeps an array of small integer objects (i.e., integers between -5 and 256, [see the doc](https://docs.python.org/2/c-api/int.html#PyInt_FromLong)).\n", + "\n", + "\n", "(*I received a comment that this is in fact a CPython artefact and must not necessarily be true in all implementations of Python!*)\n", "\n", "So the take home message is: always use \"==\" for equality, \"is\" for identity!\n", diff --git a/not_so_obvious_python_stuff.ipynb b/not_so_obvious_python_stuff.ipynb index 976101b..aea454a 100644 --- a/not_so_obvious_python_stuff.ipynb +++ b/not_so_obvious_python_stuff.ipynb @@ -1,7 +1,7 @@ { "metadata": { "name": "", - "signature": "sha256:161acdaa5124ca9e1082a3b0564e8792f64364ea91cd2cff096fdbbff37408ca" + "signature": "sha256:e20426bb66111696c7ec4f8d754946061e129b3e332a6cc2fd00331b9540d8c5" }, "nbformat": 3, "nbformat_minor": 0, @@ -212,7 +212,9 @@ "\n", "## Python reuses objects for small integers - always use \"==\" for equality, \"is\" for identity\n", "\n", - "This oddity occurs, because Python tends to stores small integers as the same object, but not so for larger ones! \n", + "This oddity occurs, because Python keeps an array of small integer objects (i.e., integers between -5 and 256, [see the doc](https://docs.python.org/2/c-api/int.html#PyInt_FromLong)).\n", + "\n", + "\n", "(*I received a comment that this is in fact a CPython artefact and must not necessarily be true in all implementations of Python!*)\n", "\n", "So the take home message is: always use \"==\" for equality, \"is\" for identity!\n",