Skip to content

Commit 5d040cc

Browse files
committed
small int clarification
1 parent b0a3c22 commit 5d040cc

2 files changed

Lines changed: 8 additions & 4 deletions

File tree

.ipynb_checkpoints/not_so_obvious_python_stuff-checkpoint.ipynb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"metadata": {
33
"name": "",
4-
"signature": "sha256:161acdaa5124ca9e1082a3b0564e8792f64364ea91cd2cff096fdbbff37408ca"
4+
"signature": "sha256:e20426bb66111696c7ec4f8d754946061e129b3e332a6cc2fd00331b9540d8c5"
55
},
66
"nbformat": 3,
77
"nbformat_minor": 0,
@@ -212,7 +212,9 @@
212212
"<a name='python_small_int'></a>\n",
213213
"## Python reuses objects for small integers - always use \"==\" for equality, \"is\" for identity\n",
214214
"\n",
215-
"This oddity occurs, because Python tends to stores small integers as the same object, but not so for larger ones! \n",
215+
"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",
216+
"\n",
217+
"\n",
216218
"(*I received a comment that this is in fact a CPython artefact and must not necessarily be true in all implementations of Python!*)\n",
217219
"\n",
218220
"So the take home message is: always use \"==\" for equality, \"is\" for identity!\n",

not_so_obvious_python_stuff.ipynb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"metadata": {
33
"name": "",
4-
"signature": "sha256:161acdaa5124ca9e1082a3b0564e8792f64364ea91cd2cff096fdbbff37408ca"
4+
"signature": "sha256:e20426bb66111696c7ec4f8d754946061e129b3e332a6cc2fd00331b9540d8c5"
55
},
66
"nbformat": 3,
77
"nbformat_minor": 0,
@@ -212,7 +212,9 @@
212212
"<a name='python_small_int'></a>\n",
213213
"## Python reuses objects for small integers - always use \"==\" for equality, \"is\" for identity\n",
214214
"\n",
215-
"This oddity occurs, because Python tends to stores small integers as the same object, but not so for larger ones! \n",
215+
"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",
216+
"\n",
217+
"\n",
216218
"(*I received a comment that this is in fact a CPython artefact and must not necessarily be true in all implementations of Python!*)\n",
217219
"\n",
218220
"So the take home message is: always use \"==\" for equality, \"is\" for identity!\n",

0 commit comments

Comments
 (0)