
{"id":4971,"date":"2024-07-11T22:18:42","date_gmt":"2024-07-12T02:18:42","guid":{"rendered":"https:\/\/ikriv.com\/blog\/?p=4971"},"modified":"2024-07-11T22:18:42","modified_gmt":"2024-07-12T02:18:42","slug":"python-assignment-in-lambdas","status":"publish","type":"post","link":"https:\/\/ikriv.com\/blog\/?p=4971","title":{"rendered":"Python: assignment in lambdas"},"content":{"rendered":"<p><b>TL;DR<\/b> Use default parameters to simulate assignment in lambdas.<\/p>\n<p>Unlike C and C++, in Python assignment is an expression, not a statement. Python lambda can only return an expression, which means that there is no way to assign values for future reuse.<\/p>\n<p>E.g., if we want to calculate (x<sup>2<\/sup> + 2x + 7) + sqrt(x<sup>2<\/sup> + 2x + 7), we can write a regular function as follows:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nfrom math import sqrt\r\ndef mymath(x):\r\n  y = x**2 + 2*x + 7\r\n  return y + sqrt(y)\r\n<\/pre>\n<p>However, when defining a lambda, we cannot simply assign a variable like this:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nfrom math import sqrt\r\n# SyntaxError: invalid syntax\r\nmymath = lambda x: (y = x**2 + 2*x + 7; y + sqrt(y))\r\n<\/pre>\n<p>Fortunately, there is a way to beat the system:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nfrom math import sqrt\r\nmymath = lambda x: (lambda y: y + sqrt(y))(x**2 + 2*x + 7)\r\n<\/pre>\n<p>Here the outer lambda creates an inner lambda, and then immediately executes it, supplying <code>x**2 + 2*x + 7<\/code> as a parameter. It is bound to <code>y<\/code>in the inner lambda and is calculated only once. This works, but the order of calculations is counterintuitive: we first evaluate the expression on the right, and then the expression on the left. We can do better with default parameter values:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nfrom math import sqrt\r\nmymath = lambda x: (lambda y = x**2 + 2*x + 7: y + sqrt(y))()\r\n<\/pre>\n<p>Here we create an inner lambda where parameter <code>y<\/code> has default value of <code>x**2+2*x+7<\/code>, and then immediately execute it supplying no arguments. <code>y<\/code> takes the default value and is calculated only once. We can even create multiple variables this way, as long as they are independent:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nfrom math import sqrt\r\nmymath = lambda x: (lambda \r\n   y = x**2 + 2*x + 7, \r\n   z = x**3 + 12: \r\n   y - z + sqrt(y + z))()\r\n<\/pre>\n<p>This looks <i>almost<\/i> like a regular function with assignments, but it&#8217;s still a lambda.<br \/>\nCredit: <a href=\"https:\/\/nedbatchelder.com\/blog\/201206\/eval_really_is_dangerous.html\">https:\/\/nedbatchelder.com\/blog\/201206\/eval_really_is_dangerous.html<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>TL;DR Use default parameters to simulate assignment in lambdas. Unlike C and C++, in Python assignment is an expression, not a statement. Python lambda can only return an expression, which <a href=\"https:\/\/ikriv.com\/blog\/?p=4971\" class=\"more-link\">[&hellip;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"Layout":"","footnotes":""},"categories":[4],"tags":[],"class_list":["entry","author-ikriv","post-4971","post","type-post","status-publish","format-standard","category-hack"],"_links":{"self":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4971","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4971"}],"version-history":[{"count":6,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4971\/revisions"}],"predecessor-version":[{"id":4977,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4971\/revisions\/4977"}],"wp:attachment":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4971"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4971"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4971"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}