{"id":1110,"date":"2013-12-07T09:15:16","date_gmt":"2013-12-06T21:15:16","guid":{"rendered":"http:\/\/p-s.co.nz\/wordpress\/?p=1110"},"modified":"2014-10-25T07:25:33","modified_gmt":"2014-10-24T19:25:33","slug":"python-tuples-are-immutable-right","status":"publish","type":"post","link":"https:\/\/p-s.co.nz\/wordpress\/python-tuples-are-immutable-right\/","title":{"rendered":"Python tuples are immutable right?"},"content":{"rendered":"<p>Tuples are immutable, unchangeable right? Well yes &#8211; sort of. The issue is easier to illustrate than describe so here goes. If we have a list, we can add new items. E.g.<\/p>\n<p><code>>>> l = [\"apple\", \"banana\", \"cucumber\"]<br \/>\n>>> l<br \/>\n['apple', 'banana', 'cucumber']<br \/>\n>>> l.append(\"date\")<br \/>\n>>> l<br \/>\n['apple', 'banana', 'cucumber', 'date']<\/code><\/p>\n<p>If we have a tuple, we can&#8217;t change which objects are contained in the tuple:<\/p>\n<p><code>>>> t = (\"apple\", \"banana\", \"cucumber\")<br \/>\n>>> t<br \/>\n('apple', 'banana', 'cucumber')<br \/>\n>>> t[3] = \"date\"<\/code><\/p>\n<p>You get TypeError: &#8216;tuple&#8217; object does not support item assignment<\/p>\n<p>So once you have a tuple, nothing will change, right? It is even called immutable so end of story right? Not quite.<\/p>\n<p>Well try this:<br \/>\n<code><br \/>\n>>> a = [\"apple\",]<br \/>\n>>> b = [\"banana\",]<br \/>\n>>> c = [\"cucumber\",]<br \/>\n>>> t = (a,b,c)<br \/>\n>>> t<br \/>\n(['apple'], ['banana'], ['cucumber'])<br \/>\n>>> b.append(\"A new banana in my immutable tuple! WAT?!\")<br \/>\n>>> t<br \/>\n(['apple'], ['banana', 'A new banana in my immutable tuple! WAT?!'], ['cucumber'])<\/code><\/p>\n<p>From the official documentation: <\/p>\n<blockquote><p>&#8220;Objects whose value can change are said to be mutable; objects whose value is unchangeable once they are created are called immutable. (The value of an immutable container object that contains a reference to a mutable object can change when the latter\u2019s value is changed; however the container is s<strong>till considered immutable, because the collection of objects it contains cannot be changed. So, immutability is not strictly the same as having an unchangeable value, it is more subtle<\/strong>.)&#8221; (<a href=\"http:\/\/docs.python.org\/2\/reference\/datamodel.html\" target=\"_blank\">Data Model<\/a>) [emphasis added]<\/p><\/blockquote>\n<p>Subtle &#8211; yes &#8211; well said. <em>Which<\/em> objects are contained in a tuple is fixed &#8211; but <em>what<\/em> those objects are is not. In practice, most tuples are containers of numbers and strings and this potential confusion never arises. Incidentally, even though tuples are immutable, they can&#8217;t be used dictionary keys if they contain mutable objects. Try it! E.g. <\/p>\n<p><code>d = {((1,2), <strong>(<\/strong>3,4<strong>)<\/strong>): 100} <\/code><br \/>\nvs<br \/>\n<code>d = {((1,2), <strong>[<\/strong>3,4<strong>]<\/strong>): 100}<\/code><\/p>\n<p>For more useful explanation of how Python handles variables, objects etc check out: <a href=\"http:\/\/www.jeffknupp.com\/blog\/2013\/02\/14\/drastically-improve-your-python-understanding-pythons-execution-model\/\" target=\"_blank\">Drastically Improve Your Python: Understanding Python&#8217;s Execution Model<\/a>. It sometimes introduces too many new ideas at once in the example code but is very helpful if you focus on the salient parts of the examples only.<\/p>\n<p>[Added later] See <a href=\"http:\/\/radar.oreilly.com\/2014\/10\/python-tuples-immutable-but-potentially-changing.html\" target=\"_blank\">Python tuples: immutable but potentially changing<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Tuples are immutable, unchangeable right? Well yes &#8211; sort of. The issue is easier to illustrate than describe so here goes. If we have a list, we can add new items. E.g. >>> l = [&#8220;apple&#8221;, &#8220;banana&#8221;, &#8220;cucumber&#8221;] >>> l &hellip; <a href=\"https:\/\/p-s.co.nz\/wordpress\/python-tuples-are-immutable-right\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11,3],"tags":[],"class_list":["post-1110","post","type-post","status-publish","format-standard","hentry","category-programming","category-python"],"_links":{"self":[{"href":"https:\/\/p-s.co.nz\/wordpress\/wp-json\/wp\/v2\/posts\/1110"}],"collection":[{"href":"https:\/\/p-s.co.nz\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/p-s.co.nz\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/p-s.co.nz\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/p-s.co.nz\/wordpress\/wp-json\/wp\/v2\/comments?post=1110"}],"version-history":[{"count":8,"href":"https:\/\/p-s.co.nz\/wordpress\/wp-json\/wp\/v2\/posts\/1110\/revisions"}],"predecessor-version":[{"id":1245,"href":"https:\/\/p-s.co.nz\/wordpress\/wp-json\/wp\/v2\/posts\/1110\/revisions\/1245"}],"wp:attachment":[{"href":"https:\/\/p-s.co.nz\/wordpress\/wp-json\/wp\/v2\/media?parent=1110"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/p-s.co.nz\/wordpress\/wp-json\/wp\/v2\/categories?post=1110"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/p-s.co.nz\/wordpress\/wp-json\/wp\/v2\/tags?post=1110"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}