{"id":29,"date":"2006-01-20T18:17:15","date_gmt":"2006-01-20T17:17:15","guid":{"rendered":"https:\/\/raphael.slinckx.net\/blog\/index.php\/2006-01-20\/fun-with-python"},"modified":"2006-06-19T22:57:14","modified_gmt":"2006-06-19T20:57:14","slug":"fun-with-python","status":"publish","type":"post","link":"https:\/\/raphael.slinckx.net\/blog\/2006-01-20\/fun-with-python","title":{"rendered":"Fun with python"},"content":{"rendered":"<p>Fun python snippets<\/p>\n<p>Here is a short list of python WTF&#8217;s i often come across in my own code, debugging<br \/>\nthose is quite hard..<\/p>\n<p>Tuples are immutable ?<\/p>\n<pre>a = ([],[])\r\n\r\ntry:\r\n# Modifying a tuple ?\r\na[0] += [1]\r\nexcept:\r\npass\r\n\r\nprint a<\/pre>\n<p>Weird list references:<\/p>\n<pre>a = [[None]*4]*3\r\na[0][0] = 1\r\n\r\nprint a<\/pre>\n<p>Import madness:<\/p>\n<pre>* in mod1.py:\r\nFOO=1\r\n\r\n* in mod2.py:\r\nimport mod1\r\ndef set_foo(i):\r\nmod1.FOO = i\r\n\r\n* in mod3.py:\r\nfrom mod1 import *\r\nimport mod2\r\nmod2.set_foo(2)\r\nprint FOO\r\n\r\nRun \"python mod3.py\"<\/pre>\n<p>Weird variable scoping:<\/p>\n<pre>* in mod1.py:\r\na = 3\r\ndef foo():\r\nreturn a\r\n\r\nif __name__ == \"__main__\":\r\nfor a in range(3):\r\nprint foo()\r\n\r\n* in mod2.py\r\nfrom mod1 import *\r\na = 6\r\nprint foo()\r\n\r\nCompare \"python mod1.py\" and \"python mod2.py\"<\/pre>\n<p>Of course these snippets are a bit convoluted, but I often find these patterns appearing randomly..<\/p>\n<p>How much of them have you got right without looking at the answer before?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Fun python snippets Here is a short list of python WTF&#8217;s i often come across in my own code, debugging those is quite hard.. Tuples are immutable ? a = ([],[]) try: # Modifying a tuple ? a[0] += [1] except: pass print a Weird list references: a = [[None]*4]*3 a[0][0] = 1 print a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,2],"tags":[35,10],"class_list":["post-29","post","type-post","status-publish","format-standard","hentry","category-planet-gnome","category-software","tag-fun","tag-python"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/raphael.slinckx.net\/blog\/wp-json\/wp\/v2\/posts\/29","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/raphael.slinckx.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/raphael.slinckx.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/raphael.slinckx.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/raphael.slinckx.net\/blog\/wp-json\/wp\/v2\/comments?post=29"}],"version-history":[{"count":0,"href":"https:\/\/raphael.slinckx.net\/blog\/wp-json\/wp\/v2\/posts\/29\/revisions"}],"wp:attachment":[{"href":"https:\/\/raphael.slinckx.net\/blog\/wp-json\/wp\/v2\/media?parent=29"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/raphael.slinckx.net\/blog\/wp-json\/wp\/v2\/categories?post=29"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/raphael.slinckx.net\/blog\/wp-json\/wp\/v2\/tags?post=29"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}