small int clarification

This commit is contained in:
rasbt 2014-04-16 12:45:52 -04:00
parent b0a3c220fd
commit 5d040cc076
2 changed files with 8 additions and 4 deletions

View File

@ -1,7 +1,7 @@
{
"metadata": {
"name": "",
"signature": "sha256:161acdaa5124ca9e1082a3b0564e8792f64364ea91cd2cff096fdbbff37408ca"
"signature": "sha256:e20426bb66111696c7ec4f8d754946061e129b3e332a6cc2fd00331b9540d8c5"
},
"nbformat": 3,
"nbformat_minor": 0,
@ -212,7 +212,9 @@
"<a name='python_small_int'></a>\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",

View File

@ -1,7 +1,7 @@
{
"metadata": {
"name": "",
"signature": "sha256:161acdaa5124ca9e1082a3b0564e8792f64364ea91cd2cff096fdbbff37408ca"
"signature": "sha256:e20426bb66111696c7ec4f8d754946061e129b3e332a6cc2fd00331b9540d8c5"
},
"nbformat": 3,
"nbformat_minor": 0,
@ -212,7 +212,9 @@
"<a name='python_small_int'></a>\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",