Skip to content

Commit f3844ea

Browse files
committed
immutable tuple upd.
1 parent 40d7d7b commit f3844ea

2 files changed

Lines changed: 126 additions & 2 deletions

File tree

.ipynb_checkpoints/not_so_obvious_python_stuff-checkpoint.ipynb

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"metadata": {
33
"name": "",
4-
"signature": "sha256:d6f2ef62f297b01c2d33ebe34af91db1e402323086d1abc1c6827557351df297"
4+
"signature": "sha256:29a120258e2d108ed5eace08e071ad866ae379b4f24fde804401ee858a2090fb"
55
},
66
"nbformat": 3,
77
"nbformat_minor": 0,
@@ -988,6 +988,68 @@
988988
"If we try to extend the list via `+=` *\"then the statement executes STORE_SUBSCR, which calls the C function PyObject_SetItem, which checks if the object supports item assignment. In our case the object is a tuple, so PyObject_SetItem throws the TypeError. Mystery solved.\"*"
989989
]
990990
},
991+
{
992+
"cell_type": "markdown",
993+
"metadata": {},
994+
"source": [
995+
"#### One more note about the `immutable` status of tuples. Tuples are famous for being immutable. However, how comes that this code works?"
996+
]
997+
},
998+
{
999+
"cell_type": "code",
1000+
"collapsed": false,
1001+
"input": [
1002+
"my_tup = (1,)\n",
1003+
"my_tup += (4,)\n",
1004+
"my_tup = my_tup + (5,)\n",
1005+
"print(my_tup)"
1006+
],
1007+
"language": "python",
1008+
"metadata": {},
1009+
"outputs": [
1010+
{
1011+
"output_type": "stream",
1012+
"stream": "stdout",
1013+
"text": [
1014+
"(1, 4, 5)\n"
1015+
]
1016+
}
1017+
],
1018+
"prompt_number": 6
1019+
},
1020+
{
1021+
"cell_type": "markdown",
1022+
"metadata": {},
1023+
"source": [
1024+
"What happens \"behind\" the curtains is that the tuple is not modified, but every time a new object is generated, which will inherit the old \"name tag\":"
1025+
]
1026+
},
1027+
{
1028+
"cell_type": "code",
1029+
"collapsed": false,
1030+
"input": [
1031+
"my_tup = (1,)\n",
1032+
"print(id(my_tup))\n",
1033+
"my_tup += (4,)\n",
1034+
"print(id(my_tup))\n",
1035+
"my_tup = my_tup + (5,)\n",
1036+
"print(id(my_tup))"
1037+
],
1038+
"language": "python",
1039+
"metadata": {},
1040+
"outputs": [
1041+
{
1042+
"output_type": "stream",
1043+
"stream": "stdout",
1044+
"text": [
1045+
"4337381840\n",
1046+
"4357415496\n",
1047+
"4357289952\n"
1048+
]
1049+
}
1050+
],
1051+
"prompt_number": 8
1052+
},
9911053
{
9921054
"cell_type": "markdown",
9931055
"metadata": {},

not_so_obvious_python_stuff.ipynb

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"metadata": {
33
"name": "",
4-
"signature": "sha256:d6f2ef62f297b01c2d33ebe34af91db1e402323086d1abc1c6827557351df297"
4+
"signature": "sha256:29a120258e2d108ed5eace08e071ad866ae379b4f24fde804401ee858a2090fb"
55
},
66
"nbformat": 3,
77
"nbformat_minor": 0,
@@ -988,6 +988,68 @@
988988
"If we try to extend the list via `+=` *\"then the statement executes STORE_SUBSCR, which calls the C function PyObject_SetItem, which checks if the object supports item assignment. In our case the object is a tuple, so PyObject_SetItem throws the TypeError. Mystery solved.\"*"
989989
]
990990
},
991+
{
992+
"cell_type": "markdown",
993+
"metadata": {},
994+
"source": [
995+
"#### One more note about the `immutable` status of tuples. Tuples are famous for being immutable. However, how comes that this code works?"
996+
]
997+
},
998+
{
999+
"cell_type": "code",
1000+
"collapsed": false,
1001+
"input": [
1002+
"my_tup = (1,)\n",
1003+
"my_tup += (4,)\n",
1004+
"my_tup = my_tup + (5,)\n",
1005+
"print(my_tup)"
1006+
],
1007+
"language": "python",
1008+
"metadata": {},
1009+
"outputs": [
1010+
{
1011+
"output_type": "stream",
1012+
"stream": "stdout",
1013+
"text": [
1014+
"(1, 4, 5)\n"
1015+
]
1016+
}
1017+
],
1018+
"prompt_number": 6
1019+
},
1020+
{
1021+
"cell_type": "markdown",
1022+
"metadata": {},
1023+
"source": [
1024+
"What happens \"behind\" the curtains is that the tuple is not modified, but every time a new object is generated, which will inherit the old \"name tag\":"
1025+
]
1026+
},
1027+
{
1028+
"cell_type": "code",
1029+
"collapsed": false,
1030+
"input": [
1031+
"my_tup = (1,)\n",
1032+
"print(id(my_tup))\n",
1033+
"my_tup += (4,)\n",
1034+
"print(id(my_tup))\n",
1035+
"my_tup = my_tup + (5,)\n",
1036+
"print(id(my_tup))"
1037+
],
1038+
"language": "python",
1039+
"metadata": {},
1040+
"outputs": [
1041+
{
1042+
"output_type": "stream",
1043+
"stream": "stdout",
1044+
"text": [
1045+
"4337381840\n",
1046+
"4357415496\n",
1047+
"4357289952\n"
1048+
]
1049+
}
1050+
],
1051+
"prompt_number": 8
1052+
},
9911053
{
9921054
"cell_type": "markdown",
9931055
"metadata": {},

0 commit comments

Comments
 (0)