{"id":1292,"date":"2022-05-30T14:30:53","date_gmt":"2022-05-30T14:30:53","guid":{"rendered":"http:\/\/www.wellformedness.com\/blog\/?p=1292"},"modified":"2022-05-30T15:54:58","modified_gmt":"2022-05-30T15:54:58","slug":"lambda-lifting-in-python","status":"publish","type":"post","link":"https:\/\/www.wellformedness.com\/blog\/lambda-lifting-in-python\/","title":{"rendered":"Lambda lifting in Python"},"content":{"rendered":"<p>Python really should have a way to lambda-lift a value\u00a0<em>e<\/em> to a no-argument callable function which returns <em>e<\/em>. Let us suppose that our\u00a0<em>e<\/em> is denoted by the variable <code>alpha<\/code>. One can approximate such a lifting by declaring <code>alpha_fnc = lambda: alpha<\/code>. Python lambdas are slow compared to true currying functionality, like provided by <a href=\"https:\/\/docs.python.org\/3\/library\/functools.html#functools.partial\"><code>functools.partial<\/code><\/a> and the functions of the <a href=\"https:\/\/docs.python.org\/3\/library\/operator.html\"><code>operator<\/code><\/a> library, but it basically works. The problem, however, is that lambda declarations in Python, unlike in, say, C++ 11, have no closure mechanism to capture the local scope, so lambda which refer to outer variables are context-dependent. The following interactive session illustrates the problem.<\/p>\n<pre>In [1]: alpha_fnc = lambda: alpha\r\n\r\nIn [2]: alpha_fnc()\r\n------------------------------------------------------------------------\r\nNameError Traceback (most recent call last)\r\nInput In [2], in ()\r\n----&gt; 1 alpha_fnc()\r\n\r\nInput In [1], in ()\r\n----&gt; 1 alpha_fnc = lambda: alpha\r\n\r\nNameError: name 'alpha' is not defined\r\n\r\nIn [3]: alpha = .5\r\n\r\nIn [4]: alpha_fnc()\r\nOut[4]: 0.5\r\n\r\nIn [5]: alpha = .4\r\n\r\nIn [6]: alpha_fnc()\r\nOut[6]: 0.4\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Python really should have a way to lambda-lift a value\u00a0e to a no-argument callable function which returns e. Let us suppose that our\u00a0e is denoted by the variable alpha. One can approximate such a lifting by declaring alpha_fnc = lambda: alpha. Python lambdas are slow compared to true currying functionality, like provided by functools.partial and &hellip; <a href=\"https:\/\/www.wellformedness.com\/blog\/lambda-lifting-in-python\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Lambda lifting in Python&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","footnotes":""},"categories":[3,8],"tags":[],"class_list":["post-1292","post","type-post","status-publish","format-standard","hentry","category-dev","category-python"],"_links":{"self":[{"href":"https:\/\/www.wellformedness.com\/blog\/wp-json\/wp\/v2\/posts\/1292","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wellformedness.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wellformedness.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wellformedness.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wellformedness.com\/blog\/wp-json\/wp\/v2\/comments?post=1292"}],"version-history":[{"count":2,"href":"https:\/\/www.wellformedness.com\/blog\/wp-json\/wp\/v2\/posts\/1292\/revisions"}],"predecessor-version":[{"id":1297,"href":"https:\/\/www.wellformedness.com\/blog\/wp-json\/wp\/v2\/posts\/1292\/revisions\/1297"}],"wp:attachment":[{"href":"https:\/\/www.wellformedness.com\/blog\/wp-json\/wp\/v2\/media?parent=1292"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wellformedness.com\/blog\/wp-json\/wp\/v2\/categories?post=1292"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wellformedness.com\/blog\/wp-json\/wp\/v2\/tags?post=1292"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}