{"id":1757,"date":"2019-05-06T02:31:18","date_gmt":"2019-05-06T10:31:18","guid":{"rendered":"http:\/\/wonghoi.humgar.com\/blog\/?p=1757"},"modified":"2025-12-01T03:13:03","modified_gmt":"2025-12-01T11:13:03","slug":"anonymous-functions-matlab-vs-lambdas-python","status":"publish","type":"post","link":"https:\/\/wonghoi.humgar.com\/blog\/2019\/05\/06\/anonymous-functions-matlab-vs-lambdas-python\/","title":{"rendered":"Anonymous Functions (MATLAB) vs Lambdas (Python)"},"content":{"rendered":"\n<p>Lambdas in Python does not play by the same rules as anonymous functions in MATLAB<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>MATLAB takes a snapshot of (capture) the workspace variables involved in the anonymous function AT the time the anonymous function handle is <strong>created<\/strong>, thus the captured values will live on afterwards (by definition a proper closure).<\/li>\n\n\n\n<li><del><strong><a href=\"https:\/\/samwize.com\/2012\/11\/05\/what-is-pythons-lambda-and-closure\/\">Lambda in Python is NOT closure<\/a><\/strong>!<\/del>\u00a0[EDIT: I&#8217;ll need to investigate the definition of <em>closure<\/em> more closely before I use the term here] The <em><strong>free<\/strong><\/em> variables involved in lambda expressions are simply read on-the-fly (aka, the last state) when the functor is <strong>executed<\/strong>.<\/li>\n<\/ul>\n\n\n\n<p>It&#8217;s kind of a mixed love-and-hate situation for both. Either design choice will be confusing for some use cases. I was at first thrown off by MATLAB&#8217;s anonymous function&#8217;s&nbsp;<em>full variable capture<\/em>&nbsp;behavior, then after I get used to it, Python&#8217;s Lambda&#8217;s non-closure tripped me. Even in the official <a href=\"https:\/\/docs.python.org\/3\/faq\/programming.html#why-do-lambdas-defined-in-a-loop-with-different-values-all-return-the-same-result\">FAQ<\/a>, it address the surprise that people are not getting what they expected creating lambdas in a for-loop.<\/p>\n\n\n\n<p>To enable capture in Python, you assign the value you wanted to capture to a lambda input argument (aka, using a <em>bound variable<\/em>&nbsp;as an intermediary and initialize it with the <em>free variable <\/em>that needs to be captured), then use the intermediary in the expression. For example:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">lambda: ser.close()      # does not capture 'ser'\nlambda s=ser: s.close()  # 'ser' is captured by s.<\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>I usually keep the usage of nested functions to the minimum, even in MATLAB, because effectively it&#8217;s kind of a compromised &#8216;global&#8217; between nested levels, or a little bit like <em>protected<\/em> classes in C++. It breaks encapsulation (intentionally) for functions in your inner circle (nest).<\/p>\n\n\n\n<p>It&#8217;s often useful for coding up GUI in MATLAB quick because you need to share access to the UI controls within the same group. For GUI that gets more complicated, I actually avoided nested functions altogether and used *appdata() to share UI object handles.<\/p>\n\n\n\n<p><strong>Functors of <a href=\"https:\/\/www.programiz.com\/python-programming\/closure#nonlocal\">nested functions<\/a> are closures in both MATLAB and Python!<\/strong> Only Lambdas in Python behave slightly differently.<\/p>\n<div class=\"pvc_clear\"><\/div><p id=\"pvc_stats_1757\" class=\"pvc_stats all  \" data-element-id=\"1757\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/wonghoi.humgar.com\/blog\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p><div class=\"pvc_clear\"><\/div>","protected":false},"excerpt":{"rendered":"<p>Lambdas in Python does not play by the same rules as anonymous functions in MATLAB It&#8217;s kind of a mixed love-and-hate situation for both. Either design choice will be confusing for some use cases. I was at first thrown off &hellip; <a href=\"https:\/\/wonghoi.humgar.com\/blog\/2019\/05\/06\/anonymous-functions-matlab-vs-lambdas-python\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_1757\" class=\"pvc_stats all  \" data-element-id=\"1757\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/wonghoi.humgar.com\/blog\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[10,34],"tags":[],"class_list":["post-1757","post","type-post","status-publish","format-standard","hentry","category-matlab","category-python"],"_links":{"self":[{"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/posts\/1757","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/comments?post=1757"}],"version-history":[{"count":4,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/posts\/1757\/revisions"}],"predecessor-version":[{"id":6774,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/posts\/1757\/revisions\/6774"}],"wp:attachment":[{"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/media?parent=1757"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/categories?post=1757"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/tags?post=1757"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}