{"id":39,"date":"2014-01-10T08:52:00","date_gmt":"2014-01-10T08:52:00","guid":{"rendered":"https:\/\/sandrock.co.za\/carl\/2014\/01\/10\/python-vs-matlab-octave-anonymous-functions\/"},"modified":"2014-01-10T08:52:00","modified_gmt":"2014-01-10T08:52:00","slug":"python-vs-matlab-octave-anonymous-functions","status":"publish","type":"post","link":"https:\/\/sandrock.co.za\/carl\/2014\/01\/python-vs-matlab-octave-anonymous-functions\/","title":{"rendered":"Python vs Matlab\/Octave: anonymous functions"},"content":{"rendered":"<p>I learned Matlab before I learned Python, and some of the patterns one learns in one language are hard to forget when you learn a new one. Scoping is a common area of misunderstanding between the two languages.<\/p>\n<p>I&#8217;m using Octave in these examples, but the language is the same in this respect. Consider the following snipped of code:<\/p>\n<pre>&gt; a = 1;\n&gt; b = 2;\n&gt; f = @(x) a*x + b;\n&gt; f(2)\nans = &nbsp;4\n&gt; a = 0;\n&gt; b = 0;\n&gt; f(2)\nans = &nbsp;4\n<\/pre>\n<p>\nWhat we see here is that Matlab\/Octave binds the values of the variables to the function defined in line 3 at the time of definition. This means changing <span style=\"font-family: Courier New, Courier, monospace\">a<\/span> and <span style=\"font-family: Courier New, Courier, monospace\">b<\/span> later doesn&#8217;t change <span style=\"font-family: Courier New, Courier, monospace\">f<\/span>. This is useful when functions are passed back and forth &#8211; you can build a function in one place and use it later without fearing that changing local variables will change the behaviour of the function. Let&#8217;s do the same thing in Python (the function definition below is completely equivalent to <span style=\"font-family: Courier New, Courier, monospace\">f = lambda x: a*x + b<\/span>):<\/p>\n<pre>a = 1\nb = 2\ndef f(x):\n    return a*x + b\nprint f(2)\na = 0\nb = 0\nprint f(2)\n<\/pre>\n<p>Output:<\/p>\n<pre>4\n0\n<\/pre>\n<p>\nIf you are used to the Matlab way, this result may be counter-intuitive, but I remember teaching students to program in Matlab, and in fact this was a common problem. Remembering that the variables are captured in Matlab was hard for some people, who expected Python&#8217;s behaviour.<\/p>\n<p>It is worth noticing that in the Python version, the names bound to f are not simply evaluated in whatever scope they are passed to, as can be seen if we define a new function (which will have its own namespace):<\/p>\n<pre>def g():\n    a = 5\n    b = 6\n    return f(2)\nprint g()\n<\/pre>\n<p>\nOutput:<br \/>\n<\/p>\n<pre>0<\/pre>\n<p>\nThe rule in Python is therefore that the names in functions are resolved when the function was defined, but that the values will only be obtained&nbsp;<i>at the time of execution of the function<\/i>. Because <span style=\"font-family: Courier New, Courier, monospace\">g<\/span> has its own namespace, that <span style=\"font-family: Courier New, Courier, monospace\">a<\/span> and <span style=\"font-family: Courier New, Courier, monospace\">b<\/span> are different from the one in the global namespace.<\/p>\n<p>In fact, it is possible to think of what is happening in a similar way if you remember how names and values work in Python. I highly recommend <a href=\"http:\/\/nedbatchelder.com\/text\/names.html\">this article<\/a> for a good overview of how this works. &nbsp;I have also found this <a href=\"http:\/\/pythontutor.com\/\">Online Python Tutor<\/a>&nbsp;very helpful to visualise how the name-value relationship works.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I learned Matlab before I learned Python, and some of the patterns one learns in one language are hard to forget when you learn a new one. Scoping is a common area of misunderstanding between the two languages. I&#8217;m using Octave in these examples, but the language is the same in this respect. Consider the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[3,4],"tags":[],"class_list":["post-39","post","type-post","status-publish","format-standard","hentry","category-programming","category-python"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/sandrock.co.za\/carl\/wp-json\/wp\/v2\/posts\/39","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sandrock.co.za\/carl\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sandrock.co.za\/carl\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sandrock.co.za\/carl\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sandrock.co.za\/carl\/wp-json\/wp\/v2\/comments?post=39"}],"version-history":[{"count":0,"href":"https:\/\/sandrock.co.za\/carl\/wp-json\/wp\/v2\/posts\/39\/revisions"}],"wp:attachment":[{"href":"https:\/\/sandrock.co.za\/carl\/wp-json\/wp\/v2\/media?parent=39"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sandrock.co.za\/carl\/wp-json\/wp\/v2\/categories?post=39"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sandrock.co.za\/carl\/wp-json\/wp\/v2\/tags?post=39"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}