immutable tuple upd.

This commit is contained in:
rasbt 2014-04-15 21:04:35 -04:00
parent 80cb86a801
commit 945885ffba
2 changed files with 60 additions and 16 deletions

View File

@ -1,7 +1,7 @@
{ {
"metadata": { "metadata": {
"name": "", "name": "",
"signature": "sha256:257a5a05fdf9f21f3a395d8edea0d549d6444deac97319a4c0c0d6478c490ece" "signature": "sha256:65ce88feaf4030991defb4ffc22e758e20be8f6dc5dc3046d5c804aa0089de06"
}, },
"nbformat": 3, "nbformat": 3,
"nbformat_minor": 0, "nbformat_minor": 0,
@ -48,7 +48,7 @@
"- [`bool` is a subclass of `int`](#bool_int)\n", "- [`bool` is a subclass of `int`](#bool_int)\n",
"- [About lambda and closures-in-a-loop pitfall](#lambda_closure)\n", "- [About lambda and closures-in-a-loop pitfall](#lambda_closure)\n",
"- [Python's LEGB scope resolution and the keywords `global` and `nonlocal`](#python_legb)\n", "- [Python's LEGB scope resolution and the keywords `global` and `nonlocal`](#python_legb)\n",
"- [When immutable Tuples aren't so immutable](#immutable_tuple)\n", "- [When mutable contents of immutable tuples aren't so mutable](#immutable_tuple)\n",
"- [List comprehensions are fast, but generators are faster!?](#list_generator)" "- [List comprehensions are fast, but generators are faster!?](#list_generator)"
] ]
}, },
@ -830,17 +830,16 @@
"<br>\n", "<br>\n",
"<br>\n", "<br>\n",
"<a name='immutable_tuple'></a>\n", "<a name='immutable_tuple'></a>\n",
"## When immutable Tuples aren't so immutable" "## When mutable contents of immutable tuples aren't so mutable"
] ]
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"As we all know, tuples are immutable objects in Python, right!?\n", "As we all know, tuples are immutable objects in Python, right!? But what happens if they contain mutable objects? \n",
"\n", "\n",
"**Note:** As a careful reader pointed out, \n", "First, let us have a look at the expected behavior: a `TypeError` is raised if we try to modify a value tuple: "
"\"The tuple is still immutable. You were unable to change it's cardinality, or trick it into pointing to a different contained object.\""
] ]
}, },
{ {
@ -870,7 +869,7 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"#### 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?" "#### 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."
] ]
}, },
{ {
@ -929,7 +928,7 @@
"source": [ "source": [
"<br>\n", "<br>\n",
"<br>\n", "<br>\n",
"However, there **IS** a way (where there should be none!) to modify our immutable tuple without raising the `TypeError`, the solution is the `.extend()`:" "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):"
] ]
}, },
{ {
@ -955,6 +954,29 @@
], ],
"prompt_number": 44 "prompt_number": 44
}, },
{
"cell_type": "code",
"collapsed": false,
"input": [
"tup = ([],)\n",
"print('tup before: ', tup)\n",
"tup[0].append(1)\n",
"print('tup after: ', tup)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"tup before: ([],)\n",
"tup after: ([1],)\n"
]
}
],
"prompt_number": 5
},
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},

View File

@ -1,7 +1,7 @@
{ {
"metadata": { "metadata": {
"name": "", "name": "",
"signature": "sha256:257a5a05fdf9f21f3a395d8edea0d549d6444deac97319a4c0c0d6478c490ece" "signature": "sha256:65ce88feaf4030991defb4ffc22e758e20be8f6dc5dc3046d5c804aa0089de06"
}, },
"nbformat": 3, "nbformat": 3,
"nbformat_minor": 0, "nbformat_minor": 0,
@ -48,7 +48,7 @@
"- [`bool` is a subclass of `int`](#bool_int)\n", "- [`bool` is a subclass of `int`](#bool_int)\n",
"- [About lambda and closures-in-a-loop pitfall](#lambda_closure)\n", "- [About lambda and closures-in-a-loop pitfall](#lambda_closure)\n",
"- [Python's LEGB scope resolution and the keywords `global` and `nonlocal`](#python_legb)\n", "- [Python's LEGB scope resolution and the keywords `global` and `nonlocal`](#python_legb)\n",
"- [When immutable Tuples aren't so immutable](#immutable_tuple)\n", "- [When mutable contents of immutable tuples aren't so mutable](#immutable_tuple)\n",
"- [List comprehensions are fast, but generators are faster!?](#list_generator)" "- [List comprehensions are fast, but generators are faster!?](#list_generator)"
] ]
}, },
@ -830,17 +830,16 @@
"<br>\n", "<br>\n",
"<br>\n", "<br>\n",
"<a name='immutable_tuple'></a>\n", "<a name='immutable_tuple'></a>\n",
"## When immutable Tuples aren't so immutable" "## When mutable contents of immutable tuples aren't so mutable"
] ]
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"As we all know, tuples are immutable objects in Python, right!?\n", "As we all know, tuples are immutable objects in Python, right!? But what happens if they contain mutable objects? \n",
"\n", "\n",
"**Note:** As a careful reader pointed out, \n", "First, let us have a look at the expected behavior: a `TypeError` is raised if we try to modify a value tuple: "
"\"The tuple is still immutable. You were unable to change it's cardinality, or trick it into pointing to a different contained object.\""
] ]
}, },
{ {
@ -870,7 +869,7 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"#### 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?" "#### 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."
] ]
}, },
{ {
@ -929,7 +928,7 @@
"source": [ "source": [
"<br>\n", "<br>\n",
"<br>\n", "<br>\n",
"However, there **IS** a way (where there should be none!) to modify our immutable tuple without raising the `TypeError`, the solution is the `.extend()`:" "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):"
] ]
}, },
{ {
@ -955,6 +954,29 @@
], ],
"prompt_number": 44 "prompt_number": 44
}, },
{
"cell_type": "code",
"collapsed": false,
"input": [
"tup = ([],)\n",
"print('tup before: ', tup)\n",
"tup[0].append(1)\n",
"print('tup after: ', tup)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"tup before: ([],)\n",
"tup after: ([1],)\n"
]
}
],
"prompt_number": 5
},
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},