|
1 | 1 | { |
2 | 2 | "metadata": { |
3 | 3 | "name": "", |
4 | | - "signature": "sha256:257a5a05fdf9f21f3a395d8edea0d549d6444deac97319a4c0c0d6478c490ece" |
| 4 | + "signature": "sha256:65ce88feaf4030991defb4ffc22e758e20be8f6dc5dc3046d5c804aa0089de06" |
5 | 5 | }, |
6 | 6 | "nbformat": 3, |
7 | 7 | "nbformat_minor": 0, |
|
48 | 48 | "- [`bool` is a subclass of `int`](#bool_int)\n", |
49 | 49 | "- [About lambda and closures-in-a-loop pitfall](#lambda_closure)\n", |
50 | 50 | "- [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", |
52 | 52 | "- [List comprehensions are fast, but generators are faster!?](#list_generator)" |
53 | 53 | ] |
54 | 54 | }, |
|
830 | 830 | "<br>\n", |
831 | 831 | "<br>\n", |
832 | 832 | "<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" |
834 | 834 | ] |
835 | 835 | }, |
836 | 836 | { |
837 | 837 | "cell_type": "markdown", |
838 | 838 | "metadata": {}, |
839 | 839 | "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", |
841 | 841 | "\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: " |
844 | 843 | ] |
845 | 844 | }, |
846 | 845 | { |
|
870 | 869 | "cell_type": "markdown", |
871 | 870 | "metadata": {}, |
872 | 871 | "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." |
874 | 873 | ] |
875 | 874 | }, |
876 | 875 | { |
|
929 | 928 | "source": [ |
930 | 929 | "<br>\n", |
931 | 930 | "<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):" |
933 | 932 | ] |
934 | 933 | }, |
935 | 934 | { |
|
955 | 954 | ], |
956 | 955 | "prompt_number": 44 |
957 | 956 | }, |
| 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 | + }, |
958 | 980 | { |
959 | 981 | "cell_type": "markdown", |
960 | 982 | "metadata": {}, |
|
0 commit comments