{"id":20520,"date":"2018-01-16T12:15:10","date_gmt":"2018-01-16T10:15:10","guid":{"rendered":"https:\/\/www.webcodegeeks.com\/?p=20520"},"modified":"2018-01-16T10:28:33","modified_gmt":"2018-01-16T08:28:33","slug":"weird-mostly-impractical-way-multi-line-lambdas-python","status":"publish","type":"post","link":"https:\/\/www.webcodegeeks.com\/python\/weird-mostly-impractical-way-multi-line-lambdas-python\/","title":{"rendered":"A Weird (and Mostly Impractical) Way to Do Multi-Line \u201cLambdas\u201d in Python"},"content":{"rendered":"<p>Some of you who follow me may have noticed a tendency of mine to \u201chack\u201d programming languages more than really <i>use<\/i> them (other than reflection via annotations in languages such as Java; I hate that stuff), and today is no different. Today, we look as using what would be normal higher-order functions as Python decorators to create new functions that encapsulate the idea of both the higher-order function as well as the passed-in function under one name.<\/p>\n<h2>Why?<\/h2>\n<p>This is the second time I\u2019ve attempted to create a way for multiline lambdas to exist in Python. The first time used the <code>with<\/code> block, as seen in the articles around <a href=\"https:\/\/programmingideaswithjake.wordpress.com\/2016\/01\/16\/mimicking-kotlin-builders-in-java-and-python\/\">creating Kotlin-like builders<\/a> <a href=\"https:\/\/programmingideaswithjake.wordpress.com\/2016\/01\/23\/kotlin-like-builders-in-java-and-python-continued-additional-parameters\/\">in Java and Python<\/a>, and <a href=\"https:\/\/www.webcodegeeks.com\/python\/multi-line-lambdas-python\/\">fully explained in its own article<\/a>.<br \/>\nYes, there\u2019s the obvious thing of defining the function just before passing it into the higher-order function, but the real problem with that is that it kind of separates the logic from its use. The function takes up at least two lines above its real context. Then its name is used in the higher-order function. This does have the benefit of being able to use a helpful name, but sometimes the code is more helpful than the name.<br \/>\nThen there\u2019s the issue with nesting. Probably the most commonly used higher-order functions out there are <code>map()<\/code> and <code>filter()<\/code>, but when they get nested, the code can get difficult to read:<\/p>\n<pre class=\"brush:py\">map(transformer, filter(predicate, list))<\/pre>\n<p>This is just one level of nesting, and it\u2019s already weird because it\u2019s difficult to tell that everything after <code>transformer<\/code> is just one parameter for <code>map()<\/code>. One thing that can help is new lines and indentation, I guess.<\/p>\n<pre class=\"brush:py\">map(transformer,\r\n    filter(predicate, list))<\/pre>\n<p>But that opens up a whole can of worms about <i>how<\/i> to do the indenting. I\u2019m sure some of you looked at that could and thought \u201cI would do that differently\u201d. I went through a few options of my own before settling on that.<br \/>\nSo, yeah, I think about this idea a lot, and here\u2019s another hacky idea I came up with.<\/p>\n<h2>Decorators<\/h2>\n<p><a href=\"http:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2018\/01\/aliens-decorators.jpg\"><img decoding=\"async\" class=\"aligncenter wp-image-20525 size-medium\" src=\"http:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2018\/01\/aliens-decorators-300x262.jpg\" alt=\"\" width=\"300\" height=\"262\" srcset=\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2018\/01\/aliens-decorators-300x262.jpg 300w, https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2018\/01\/aliens-decorators.jpg 500w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>The thing is, we already have a special syntax for passing multi-line functions into a higher-order function, and that\u2019s the decorator syntax. The part of decorators that makes them a less than ideal solution is the fact that they don\u2019t return anything; they\u2019re a statement that assigns the result of the decoration to the name of the decorated function.<br \/>\nNow, this <i>can<\/i> be used to your advantage, but we\u2019ll start without that because I didn\u2019t think of how that could be useful until I wrote that out, and we\u2019ll go over how my ideas evolved along the way.<\/p>\n<h2><b>T<\/b>he Base Code<\/h2>\n<p>First, we\u2019ll look at the code we\u2019ll be working with for the examples. We will be using <code>map()<\/code> and <code>filter()<\/code> over a list of strings. First it will filter out the strings that aren\u2019t \u201cvalid usernames\u201d, then it will map them to a gmail address (simply adds \u201c@gmail.com\u201d to the end).<br \/>\nSo the basic code will be:<\/p>\n<pre class=\"brush:py\">def is_legal_username(username):\r\n    # not important\r\n    return bool_result\r\n\r\ndef to_gmail_addr(username):\r\n    # not important\r\n    return gmail_addr<\/pre>\n<p>And using it would look like this:<\/p>\n<pre class=\"brush:py\">map(to_gmail_addr, filter(is_legal_username, usernames))<\/pre>\n<h2>Attempt 1 \u2013 Partial Definitions<\/h2>\n<p>In my first attempt, I effectively create decorators designed to do partial application of <code>map()<\/code> and <code>filter()<\/code>. I originally wrote them like normal decorators, but when I realized what they did, I simplified them (don\u2019t forget to import <code>functools<\/code> to have access to <code>partial()<\/code>:<\/p>\n<pre class=\"brush:py\">def filterer(func):\r\n    return partial(filter, func)\r\n\r\ndef mapper(func):\r\n    return partial(map, func)<\/pre>\n<p>I then applied these to the <code>is_legal_username()<\/code> and <code>to_gmail_addr()<\/code> functions.<\/p>\n<pre class=\"brush:py\">@filterer\r\ndef is_legal_username(username):\r\n    ...\r\n\r\n@mapper\r\ndef to_gmail_addr(username):\r\n    ...<\/pre>\n<p>Now we can filter and map like this:<\/p>\n<pre class=\"brush:py\">to_gmail_addr(is_legal_username(usernames))<\/pre>\n<p>This is much easier to read, but the names imply working on singular items (which is what the originals do). So, change the name of the functions to <code>legal_usernames()<\/code> and <code>as_gmail_addresses()<\/code>:<\/p>\n<pre class=\"brush:py\">@filterer\r\ndef legal_usernames(username):\r\n    # not important\r\n    return bool_result\r\n\r\n@mapper\r\ndef as_gmail_addresses(username):\r\n    # not important\r\n    return gmail_addr<\/pre>\n<p>Which makes the usage look like this:<\/p>\n<pre class=\"brush:py\">as_gmail_addresses(legal_usernames(usernames))<\/pre>\n<p>One could argue that you could just use the old functions and reassign partials, like this:<\/p>\n<pre class=\"brush:py\">legal_usernames = partial(filter, is_legal_username)\r\nas_gmail_addresses = partial(map, to_gmail_addr)<\/pre>\n<p>But the idea is based around replacing lambdas, so the base functions are only used in this context anyway. Using the decorators (assuming you already have them defined somewhere) allows us to use locality better to show that <code>legal_usernames()<\/code> is a function used for filtering while also immediately showing the predicate used for the filtering.<\/p>\n<h2>Attempt 2 \u2013 Off the Rails<\/h2>\n<p>Disclaimer: This one is excessive and pretty crazy. You can skip to attempt 3 if you\u2019d like. A part of me still didn\u2019t like that old idea because you define a function that operates on a per-element basis, but it gets transformed into a function that operates on an iterable. What if we liked the look of <code>as_gmail_addresses(legal_usernames(usernames))<\/code> over <code>map(to_gmail_addr, filter(is_legal_username, usernames))<\/code> but already had the functions defined and also didn\u2019t want to clutter our code with <code>partial<\/code> calls; I feel that calls to <code>partial()<\/code> should only exist within code that is already meant to be doing something \u201cmagical\u201d (i.e. in decorators, and other metaprogramming features) or else I feel like I failed to design the code properly. Well, instead of the decorator simply redefining the function, it creates a partial filter or map, but registers it somewhere and leaves the original function along to be used as-is. Well, I thought of a surprisingly easy way to get closer to the look we want (I can get way closer or even exactly what we want, but that gets progressively more complicated\/\u201dmagical\u201d, so we\u2019ll stop at this level). First, we\u2019ll create a class that allows us to extend our normal higher-order functions. I\u2019ll explain everything in the class as we use them.<\/p>\n<pre class=\"brush:py\">class Extended:\r\n    def __init__(self, func):\r\n        self.func = func\r\n        self.extensions = {}\r\n\r\n    def __call__(self, *args, **kwargs):\r\n        return self.func(*args, **kwargs)\r\n\r\n    def __getattr__(self, item):\r\n        return self.extensions[item]\r\n\r\n    def add_extension(self, name, func):\r\n        self.extensions[name] = func<\/pre>\n<p>Then we can prepare our own versions of <code>map()<\/code> and <code>filter()<\/code> by extending them.<\/p>\n<pre class=\"brush:py\">filter = Extended(filter)\r\nmap = Extended(map)<\/pre>\n<p>And the last thing to prepare is the decorators:<\/p>\n<pre class=\"brush:py\">def mapper(func):\r\n    ext = partial(map, func)\r\n    map.add_extension(func.__name__, func)\r\n    return func\r\n\r\ndef filterer(func):\r\n    ext = partial(filter, func)\r\n    filter.add_extension(func.__name__, func)\r\n    return func<\/pre>\n<p>Now we decorate the single-element functions just like we did at the beginning:<\/p>\n<pre class=\"brush:py\">@filterer\r\ndef is_legal_username(username):\r\n    ...\r\n\r\n@mapper\r\ndef to_gmail_addr(username):\r\n    ...<\/pre>\n<p>Which allows us to call them like so:<\/p>\n<pre class=\"brush:py\">map.to_gmail_addr(filter.is_legal_username(usernames))<\/pre>\n<p>Getting the names of the actual filtering and mapping functions outside of the parens is what really helps to clean it up, I feel.<br \/>\nAdditional disclaimer: When going through the ideas on my own before writing this post, this is not what I came up with. Instead, I designed a class that would work similarly to Java\u2019s <code>Stream<\/code> or Kotlin\u2019s <code>Sequence<\/code>, but where it could register new methods on it similarly to how we did here, but the biggest advantage is that now we no longer need to nest calls <i>at all<\/i>:<\/p>\n<pre class=\"brush:py\">Sequence(usernames).legal_usernames().as_gmail_addresses()<\/pre>\n<p>If you like this idea, I leave it to you to figure out (or you can bug me, and I\u2019ll send you the code).<\/p>\n<h2>Attempt 3 \u2013 Use the Result<\/h2>\n<p>In this last idea, we actually use the decorated functions as lambdas because the name we use for the defined function is <i>actually<\/i> the name of the <i>result<\/i> of the call to the higher-order function.<br \/>\nThis requires <code>filter()<\/code> and <code>map()<\/code> to be defined differently, so I\u2019ll make my own named <code>_map()<\/code> and <code>_filter()<\/code>.<\/p>\n<pre class=\"brush:py\">def _map(iterable):\r\n    return partial(map, iterable=iterable)\r\n\r\ndef _filter(iterable):\r\n    return partial(filter, iterable=iterable)<\/pre>\n<p>Those certainly don\u2019t look like normal decorators, and it\u2019s because they\u2019re <i>not<\/i> normal decorators. They\u2019re not designed to return a new function; they return the result of calling <code>filter()<\/code>\/<code>map()<\/code> with the functions and iterables they want. Which makes their usage look like this:<\/p>\n<pre class=\"brush:py\">@filter(usernames)\r\ndef legal_usernames(username):\r\n    ...\r\n\r\n@map(legal_usernames)\r\ndef gmail_addresses(username):\r\n    ...<\/pre>\n<p>Now, you have a variable called gmail_addresses that is an iterable created by filtering and mapping using the defined functions. The super awesome parts of this are:<\/p>\n<ol>\n<li>It doesn\u2019t do nesting, and is therefore written in the order that it happens.<\/li>\n<li>All three parts are integrated together. You already know that a decorator applies and is attached to the function defined below it, plus the actual result of the call is given its name here and now. Locality, people! Locality!<\/li>\n<\/ol>\n<p>If you\u2019re trying to actually write one-off \u201clambdas\u201d, I recommend this tactic over the others. To make the idea even more feasible, consider writing your higher-order functions to be called normally <i>or <\/i>as a decorator. As an example, the earlier <code>_filter()<\/code> function could be rewritten like this:<\/p>\n<pre class=\"brush:py\">def _filter(iterable, predicate=None):\r\n    if predicate is None:\r\n        return partial(filter, iterable=iterable)\r\n    else:\r\n        return filter(predicate, iterable)<\/pre>\n<p>Or, if you\u2019d like to preserve the original order of parameters when called normally, you could do it like this:<\/p>\n<pre class=\"brush:py\">def _filter(predicate=None, *, over):\r\n    if predicate is None:\r\n        return partial(filter, iterable=over)\r\n    else:\r\n        return filter(predicate, over)<\/pre>\n<p>I chose to name the iterable as \u201cover\u201d so that calling it would read like \u201cfilter over <i>this<\/i> with <i>this<\/i>\u201c.<\/p>\n<pre class=\"brush:py\">@filter(over=usernames)\r\ndef \u2026<\/pre>\n<p>You can use whatever name you want. And since you can\u2019t place non-default args in front of default ones, you have to make the others be named-only arguments.<\/p>\n<h2>Outro<\/h2>\n<p>I have too many thoughts on all of this to share it will all of you, and writing this long of an article has really zapped my mental energy for the day. Thanks for reading, and I hope you\u2019ll come back.<\/p>\n<div class=\"attribution\">\n<table>\n<tbody>\n<tr>\n<td>Published on Web Code Geeks with permission by Jacob Zimmerman, partner at our <a href=\"http:\/\/www.webcodegeeks.com\/join-us\/wcg\/\" target=\"_blank\" rel=\"noopener\">WCG program<\/a>. See the original article here: <a href=\"https:\/\/programmingideaswithjake.wordpress.com\/2018\/01\/13\/a-weird-and-mostly-impractical-way-to-do-multi-line-lambdas-in-python\/\" target=\"_blank\" rel=\"noopener\">A Weird (and Mostly Impractical) Way to Do Multi-Line \u201cLambdas\u201d in Python<\/a><\/p>\n<p>Opinions expressed by Web Code Geeks contributors are their own.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Some of you who follow me may have noticed a tendency of mine to \u201chack\u201d programming languages more than really use them (other than reflection via annotations in languages such as Java; I hate that stuff), and today is no different. Today, we look as using what would be normal higher-order functions as Python decorators &hellip;<\/p>\n","protected":false},"author":51,"featured_media":1651,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[53],"tags":[],"class_list":["post-20520","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>A Weird (and Mostly Impractical) Way to Do Multi-Line \u201cLambdas\u201d in Python - Web Code Geeks - 2026<\/title>\n<meta name=\"description\" content=\"Some of you who follow me may have noticed a tendency of mine to \u201chack\u201d programming languages more than really use them (other than reflection via\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.webcodegeeks.com\/python\/weird-mostly-impractical-way-multi-line-lambdas-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"A Weird (and Mostly Impractical) Way to Do Multi-Line \u201cLambdas\u201d in Python - Web Code Geeks - 2026\" \/>\n<meta property=\"og:description\" content=\"Some of you who follow me may have noticed a tendency of mine to \u201chack\u201d programming languages more than really use them (other than reflection via\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.webcodegeeks.com\/python\/weird-mostly-impractical-way-multi-line-lambdas-python\/\" \/>\n<meta property=\"og:site_name\" content=\"Web Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webcodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2018-01-16T10:15:10+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Jacob Zimmerman\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/jacobz_20\" \/>\n<meta name=\"twitter:site\" content=\"@webcodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Jacob Zimmerman\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"9 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.webcodegeeks.com\/python\/weird-mostly-impractical-way-multi-line-lambdas-python\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/python\/weird-mostly-impractical-way-multi-line-lambdas-python\/\"},\"author\":{\"name\":\"Jacob Zimmerman\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/f54a53cfb8523f4ef6012aa63f075c39\"},\"headline\":\"A Weird (and Mostly Impractical) Way to Do Multi-Line \u201cLambdas\u201d in Python\",\"datePublished\":\"2018-01-16T10:15:10+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/python\/weird-mostly-impractical-way-multi-line-lambdas-python\/\"},\"wordCount\":1445,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/python\/weird-mostly-impractical-way-multi-line-lambdas-python\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg\",\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.webcodegeeks.com\/python\/weird-mostly-impractical-way-multi-line-lambdas-python\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.webcodegeeks.com\/python\/weird-mostly-impractical-way-multi-line-lambdas-python\/\",\"url\":\"https:\/\/www.webcodegeeks.com\/python\/weird-mostly-impractical-way-multi-line-lambdas-python\/\",\"name\":\"A Weird (and Mostly Impractical) Way to Do Multi-Line \u201cLambdas\u201d in Python - Web Code Geeks - 2026\",\"isPartOf\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/python\/weird-mostly-impractical-way-multi-line-lambdas-python\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/python\/weird-mostly-impractical-way-multi-line-lambdas-python\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg\",\"datePublished\":\"2018-01-16T10:15:10+00:00\",\"description\":\"Some of you who follow me may have noticed a tendency of mine to \u201chack\u201d programming languages more than really use them (other than reflection via\",\"breadcrumb\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/python\/weird-mostly-impractical-way-multi-line-lambdas-python\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.webcodegeeks.com\/python\/weird-mostly-impractical-way-multi-line-lambdas-python\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/python\/weird-mostly-impractical-way-multi-line-lambdas-python\/#primaryimage\",\"url\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg\",\"contentUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.webcodegeeks.com\/python\/weird-mostly-impractical-way-multi-line-lambdas-python\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.webcodegeeks.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python\",\"item\":\"https:\/\/www.webcodegeeks.com\/category\/python\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"A Weird (and Mostly Impractical) Way to Do Multi-Line \u201cLambdas\u201d in Python\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#website\",\"url\":\"https:\/\/www.webcodegeeks.com\/\",\"name\":\"Web Code Geeks\",\"description\":\"Web Developers Resource Center\",\"publisher\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.webcodegeeks.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\/\/www.webcodegeeks.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"contentUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webcodegeeks\",\"https:\/\/x.com\/webcodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/f54a53cfb8523f4ef6012aa63f075c39\",\"name\":\"Jacob Zimmerman\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/2dfdd9e2d35ed2224faf73968f8c597b5489fc345287e06e2571d3935a6bcc86?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/2dfdd9e2d35ed2224faf73968f8c597b5489fc345287e06e2571d3935a6bcc86?s=96&d=mm&r=g\",\"caption\":\"Jacob Zimmerman\"},\"description\":\"Jacob is a certified Java programmer (level 1) and Python enthusiast. He loves to solve large problems with programming and considers himself pretty good at design.\",\"sameAs\":[\"https:\/\/programmingideaswithjake.wordpress.com\/\",\"https:\/\/x.com\/https:\/\/twitter.com\/jacobz_20\"],\"url\":\"https:\/\/www.webcodegeeks.com\/author\/jacob-zimmerman\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"A Weird (and Mostly Impractical) Way to Do Multi-Line \u201cLambdas\u201d in Python - Web Code Geeks - 2026","description":"Some of you who follow me may have noticed a tendency of mine to \u201chack\u201d programming languages more than really use them (other than reflection via","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.webcodegeeks.com\/python\/weird-mostly-impractical-way-multi-line-lambdas-python\/","og_locale":"en_US","og_type":"article","og_title":"A Weird (and Mostly Impractical) Way to Do Multi-Line \u201cLambdas\u201d in Python - Web Code Geeks - 2026","og_description":"Some of you who follow me may have noticed a tendency of mine to \u201chack\u201d programming languages more than really use them (other than reflection via","og_url":"https:\/\/www.webcodegeeks.com\/python\/weird-mostly-impractical-way-multi-line-lambdas-python\/","og_site_name":"Web Code Geeks","article_publisher":"https:\/\/www.facebook.com\/webcodegeeks","article_published_time":"2018-01-16T10:15:10+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg","type":"image\/jpeg"}],"author":"Jacob Zimmerman","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/jacobz_20","twitter_site":"@webcodegeeks","twitter_misc":{"Written by":"Jacob Zimmerman","Est. reading time":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.webcodegeeks.com\/python\/weird-mostly-impractical-way-multi-line-lambdas-python\/#article","isPartOf":{"@id":"https:\/\/www.webcodegeeks.com\/python\/weird-mostly-impractical-way-multi-line-lambdas-python\/"},"author":{"name":"Jacob Zimmerman","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/f54a53cfb8523f4ef6012aa63f075c39"},"headline":"A Weird (and Mostly Impractical) Way to Do Multi-Line \u201cLambdas\u201d in Python","datePublished":"2018-01-16T10:15:10+00:00","mainEntityOfPage":{"@id":"https:\/\/www.webcodegeeks.com\/python\/weird-mostly-impractical-way-multi-line-lambdas-python\/"},"wordCount":1445,"commentCount":0,"publisher":{"@id":"https:\/\/www.webcodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.webcodegeeks.com\/python\/weird-mostly-impractical-way-multi-line-lambdas-python\/#primaryimage"},"thumbnailUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg","articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.webcodegeeks.com\/python\/weird-mostly-impractical-way-multi-line-lambdas-python\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.webcodegeeks.com\/python\/weird-mostly-impractical-way-multi-line-lambdas-python\/","url":"https:\/\/www.webcodegeeks.com\/python\/weird-mostly-impractical-way-multi-line-lambdas-python\/","name":"A Weird (and Mostly Impractical) Way to Do Multi-Line \u201cLambdas\u201d in Python - Web Code Geeks - 2026","isPartOf":{"@id":"https:\/\/www.webcodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.webcodegeeks.com\/python\/weird-mostly-impractical-way-multi-line-lambdas-python\/#primaryimage"},"image":{"@id":"https:\/\/www.webcodegeeks.com\/python\/weird-mostly-impractical-way-multi-line-lambdas-python\/#primaryimage"},"thumbnailUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg","datePublished":"2018-01-16T10:15:10+00:00","description":"Some of you who follow me may have noticed a tendency of mine to \u201chack\u201d programming languages more than really use them (other than reflection via","breadcrumb":{"@id":"https:\/\/www.webcodegeeks.com\/python\/weird-mostly-impractical-way-multi-line-lambdas-python\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.webcodegeeks.com\/python\/weird-mostly-impractical-way-multi-line-lambdas-python\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/python\/weird-mostly-impractical-way-multi-line-lambdas-python\/#primaryimage","url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg","contentUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/www.webcodegeeks.com\/python\/weird-mostly-impractical-way-multi-line-lambdas-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.webcodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Python","item":"https:\/\/www.webcodegeeks.com\/category\/python\/"},{"@type":"ListItem","position":3,"name":"A Weird (and Mostly Impractical) Way to Do Multi-Line \u201cLambdas\u201d in Python"}]},{"@type":"WebSite","@id":"https:\/\/www.webcodegeeks.com\/#website","url":"https:\/\/www.webcodegeeks.com\/","name":"Web Code Geeks","description":"Web Developers Resource Center","publisher":{"@id":"https:\/\/www.webcodegeeks.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.webcodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.webcodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.webcodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webcodegeeks","https:\/\/x.com\/webcodegeeks"]},{"@type":"Person","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/f54a53cfb8523f4ef6012aa63f075c39","name":"Jacob Zimmerman","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/2dfdd9e2d35ed2224faf73968f8c597b5489fc345287e06e2571d3935a6bcc86?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/2dfdd9e2d35ed2224faf73968f8c597b5489fc345287e06e2571d3935a6bcc86?s=96&d=mm&r=g","caption":"Jacob Zimmerman"},"description":"Jacob is a certified Java programmer (level 1) and Python enthusiast. He loves to solve large problems with programming and considers himself pretty good at design.","sameAs":["https:\/\/programmingideaswithjake.wordpress.com\/","https:\/\/x.com\/https:\/\/twitter.com\/jacobz_20"],"url":"https:\/\/www.webcodegeeks.com\/author\/jacob-zimmerman\/"}]}},"_links":{"self":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts\/20520","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/users\/51"}],"replies":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/comments?post=20520"}],"version-history":[{"count":0,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts\/20520\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/media\/1651"}],"wp:attachment":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/media?parent=20520"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/categories?post=20520"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/tags?post=20520"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}