Skip to content

Commit 945885f

Browse files
committed
immutable tuple upd.
1 parent 80cb86a commit 945885f

2 files changed

Lines changed: 60 additions & 16 deletions

File tree

.ipynb_checkpoints/not_so_obvious_python_stuff-checkpoint.ipynb

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"metadata": {
33
"name": "",
4-
"signature": "sha256:257a5a05fdf9f21f3a395d8edea0d549d6444deac97319a4c0c0d6478c490ece"
4+
"signature": "sha256:65ce88feaf4030991defb4ffc22e758e20be8f6dc5dc3046d5c804aa0089de06"
55
},
66
"nbformat": 3,
77
"nbformat_minor": 0,
@@ -48,7 +48,7 @@
4848
"- [`bool` is a subclass of `int`](#bool_int)\n",
4949
"- [About lambda and closures-in-a-loop pitfall](#lambda_closure)\n",
5050
"- [Python's LEGB scope resolution and the keywords `global` and `nonlocal`](#python_legb)\n",
51-
"- [When immutable Tuples aren't so immutable](#immutable_tuple)\n",
51+
"- [When mutable contents of immutable tuples aren't so mutable](#immutable_tuple)\n",
5252
"- [List comprehensions are fast, but generators are faster!?](#list_generator)"
5353
]
5454
},
@@ -830,17 +830,16 @@
830830
"<br>\n",
831831
"<br>\n",
832832
"<a name='immutable_tuple'></a>\n",
833-
"## When immutable Tuples aren't so immutable"
833+
"## When mutable contents of immutable tuples aren't so mutable"
834834
]
835835
},
836836
{
837837
"cell_type": "markdown",
838838
"metadata": {},
839839
"source": [
840-
"As we all know, tuples are immutable objects in Python, right!?\n",
840+
"As we all know, tuples are immutable objects in Python, right!? But what happens if they contain mutable objects? \n",
841841
"\n",
842-
"**Note:** As a careful reader pointed out, \n",
843-
"\"The tuple is still immutable. You were unable to change it's cardinality, or trick it into pointing to a different contained object.\""
842+
"First, let us have a look at the expected behavior: a `TypeError` is raised if we try to modify a value tuple: "
844843
]
845844
},
846845
{
@@ -870,7 +869,7 @@
870869
"cell_type": "markdown",
871870
"metadata": {},
872871
"source": [
873-
"#### But what if we put a mutable object into the immutable tuple? Well, modification works, but we **also** get a type error, funny, isn't it?"
872+
"#### But what if we put a mutable object into the immutable tuple? Well, modification works, but we **also** get a `TypeError` at the same time."
874873
]
875874
},
876875
{
@@ -929,7 +928,7 @@
929928
"source": [
930929
"<br>\n",
931930
"<br>\n",
932-
"However, there **IS** a way (where there should be none!) to modify our immutable tuple without raising the `TypeError`, the solution is the `.extend()`:"
931+
"However, **there are ways** to modify the mutable contents of the tuple without raising the `TypeError`, the solution is the `.extend()` method, or alternatively `.append()` (for lists):"
933932
]
934933
},
935934
{
@@ -955,6 +954,29 @@
955954
],
956955
"prompt_number": 44
957956
},
957+
{
958+
"cell_type": "code",
959+
"collapsed": false,
960+
"input": [
961+
"tup = ([],)\n",
962+
"print('tup before: ', tup)\n",
963+
"tup[0].append(1)\n",
964+
"print('tup after: ', tup)"
965+
],
966+
"language": "python",
967+
"metadata": {},
968+
"outputs": [
969+
{
970+
"output_type": "stream",
971+
"stream": "stdout",
972+
"text": [
973+
"tup before: ([],)\n",
974+
"tup after: ([1],)\n"
975+
]
976+
}
977+
],
978+
"prompt_number": 5
979+
},
958980
{
959981
"cell_type": "markdown",
960982
"metadata": {},

not_so_obvious_python_stuff.ipynb

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"metadata": {
33
"name": "",
4-
"signature": "sha256:257a5a05fdf9f21f3a395d8edea0d549d6444deac97319a4c0c0d6478c490ece"
4+
"signature": "sha256:65ce88feaf4030991defb4ffc22e758e20be8f6dc5dc3046d5c804aa0089de06"
55
},
66
"nbformat": 3,
77
"nbformat_minor": 0,
@@ -48,7 +48,7 @@
4848
"- [`bool` is a subclass of `int`](#bool_int)\n",
4949
"- [About lambda and closures-in-a-loop pitfall](#lambda_closure)\n",
5050
"- [Python's LEGB scope resolution and the keywords `global` and `nonlocal`](#python_legb)\n",
51-
"- [When immutable Tuples aren't so immutable](#immutable_tuple)\n",
51+
"- [When mutable contents of immutable tuples aren't so mutable](#immutable_tuple)\n",
5252
"- [List comprehensions are fast, but generators are faster!?](#list_generator)"
5353
]
5454
},
@@ -830,17 +830,16 @@
830830
"<br>\n",
831831
"<br>\n",
832832
"<a name='immutable_tuple'></a>\n",
833-
"## When immutable Tuples aren't so immutable"
833+
"## When mutable contents of immutable tuples aren't so mutable"
834834
]
835835
},
836836
{
837837
"cell_type": "markdown",
838838
"metadata": {},
839839
"source": [
840-
"As we all know, tuples are immutable objects in Python, right!?\n",
840+
"As we all know, tuples are immutable objects in Python, right!? But what happens if they contain mutable objects? \n",
841841
"\n",
842-
"**Note:** As a careful reader pointed out, \n",
843-
"\"The tuple is still immutable. You were unable to change it's cardinality, or trick it into pointing to a different contained object.\""
842+
"First, let us have a look at the expected behavior: a `TypeError` is raised if we try to modify a value tuple: "
844843
]
845844
},
846845
{
@@ -870,7 +869,7 @@
870869
"cell_type": "markdown",
871870
"metadata": {},
872871
"source": [
873-
"#### But what if we put a mutable object into the immutable tuple? Well, modification works, but we **also** get a type error, funny, isn't it?"
872+
"#### But what if we put a mutable object into the immutable tuple? Well, modification works, but we **also** get a `TypeError` at the same time."
874873
]
875874
},
876875
{
@@ -929,7 +928,7 @@
929928
"source": [
930929
"<br>\n",
931930
"<br>\n",
932-
"However, there **IS** a way (where there should be none!) to modify our immutable tuple without raising the `TypeError`, the solution is the `.extend()`:"
931+
"However, **there are ways** to modify the mutable contents of the tuple without raising the `TypeError`, the solution is the `.extend()` method, or alternatively `.append()` (for lists):"
933932
]
934933
},
935934
{
@@ -955,6 +954,29 @@
955954
],
956955
"prompt_number": 44
957956
},
957+
{
958+
"cell_type": "code",
959+
"collapsed": false,
960+
"input": [
961+
"tup = ([],)\n",
962+
"print('tup before: ', tup)\n",
963+
"tup[0].append(1)\n",
964+
"print('tup after: ', tup)"
965+
],
966+
"language": "python",
967+
"metadata": {},
968+
"outputs": [
969+
{
970+
"output_type": "stream",
971+
"stream": "stdout",
972+
"text": [
973+
"tup before: ([],)\n",
974+
"tup after: ([1],)\n"
975+
]
976+
}
977+
],
978+
"prompt_number": 5
979+
},
958980
{
959981
"cell_type": "markdown",
960982
"metadata": {},

0 commit comments

Comments
 (0)