{"id":629,"date":"2007-08-04T10:26:14","date_gmt":"2007-08-04T02:26:14","guid":{"rendered":"http:\/\/nick.onetwenty.org\/index.php\/2007\/08\/04\/default-arguments-in-python\/"},"modified":"2007-08-04T10:27:21","modified_gmt":"2007-08-04T02:27:21","slug":"default-arguments-in-python","status":"publish","type":"post","link":"https:\/\/nick.onetwenty.org\/index.php\/2007\/08\/04\/default-arguments-in-python\/","title":{"rendered":"Default arguments in Python"},"content":{"rendered":"<p><em><strong>Note<\/strong>: This post is pretty much only of interest (and will only make sense) to programmers.<\/em><\/p>\n<p>I&#8217;ve been using Python a lot for around 6 months now, and just yesterday I run into an apparently common gotcha. I&#8217;ve boiled it down to a very basic example.<\/p>\n<p>First, let&#8217;s define a simple function with a default argument:<\/p>\n<pre>\r\ndef f( a = [] ):\r\n    a.append(1)\r\n    print a\r\n<\/pre>\n<p>Now, let&#8217;s call it twice and see what happens:<\/p>\n<pre>\r\n> f()\r\n[1]\r\n> f()\r\n[1, 1]\r\n<\/pre>\n<p>Probably not what you expected? From the POV of a C programmer, this just means that Python default arguments map to something like:<\/p>\n<pre>\r\n    static int a[] = {};\r\n<\/pre>\n<p>rather than:<\/p>\n<pre>\r\n    int a[] = {};\r\n<\/pre>\n<p>at the start of a function. (For those whom may not know; yes, C allows for static variables nested within functions.)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Note: This post is pretty much only of interest (and will only make sense) to programmers. I&#8217;ve been using Python a lot for around 6 months now, and just yesterday I run into an apparently common gotcha. I&#8217;ve boiled it down to a very basic example. First, let&#8217;s define a simple function with a default &hellip; <a href=\"https:\/\/nick.onetwenty.org\/index.php\/2007\/08\/04\/default-arguments-in-python\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Default arguments in Python&#8221;<\/span><\/a><\/p>\n","protected":false},"author":67,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false},"categories":[1],"tags":[],"jetpack_featured_media_url":"","jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/paLsRH-a9","_links":{"self":[{"href":"https:\/\/nick.onetwenty.org\/index.php\/wp-json\/wp\/v2\/posts\/629"}],"collection":[{"href":"https:\/\/nick.onetwenty.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nick.onetwenty.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nick.onetwenty.org\/index.php\/wp-json\/wp\/v2\/users\/67"}],"replies":[{"embeddable":true,"href":"https:\/\/nick.onetwenty.org\/index.php\/wp-json\/wp\/v2\/comments?post=629"}],"version-history":[{"count":0,"href":"https:\/\/nick.onetwenty.org\/index.php\/wp-json\/wp\/v2\/posts\/629\/revisions"}],"wp:attachment":[{"href":"https:\/\/nick.onetwenty.org\/index.php\/wp-json\/wp\/v2\/media?parent=629"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nick.onetwenty.org\/index.php\/wp-json\/wp\/v2\/categories?post=629"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nick.onetwenty.org\/index.php\/wp-json\/wp\/v2\/tags?post=629"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}