<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[CloudTech Pro]]></title><description><![CDATA[CloudTech Pro]]></description><link>https://cloudtech.pro</link><generator>RSS for Node</generator><lastBuildDate>Tue, 07 Apr 2026 20:06:50 GMT</lastBuildDate><atom:link href="https://cloudtech.pro/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Python Anonymous Function]]></title><description><![CDATA[When a function is created with the key word lambda in python its called anonymous function. They don't use the traditional function key word def. Anonymous function (aka lambda) is designed for coding simple functions whereas def used for larger tas...]]></description><link>https://cloudtech.pro/python-anonymous-function</link><guid isPermaLink="true">https://cloudtech.pro/python-anonymous-function</guid><category><![CDATA[Python]]></category><dc:creator><![CDATA[Biswa Singh]]></dc:creator><pubDate>Mon, 09 Sep 2024 05:38:54 GMT</pubDate><content:encoded><![CDATA[<p>When a function is created with the key word <strong>lambda</strong> in python its called anonymous function. They don't use the traditional function key word <strong>def</strong>. Anonymous function (aka lambda) is designed for coding simple functions whereas <strong>def</strong> used for larger tasks. The name lambda comes from the Lisp programming language. Also don't confuse Python Lambda(anonymous) functions with AWS lambda function as the later is used as serverless compute infrastructure. If you come from C++ background then for the sake of your understanding you can correlate lambdas with C++ inline functions.</p>
<h3 id="heading-syntax">Syntax</h3>
<pre><code class="lang-plaintext">lambda arg1, arg2,... argn: expression
</code></pre>
<h3 id="heading-example">Example</h3>
<pre><code class="lang-python"><span class="hljs-comment"># Simple sum anonymous function definition</span>
sum = <span class="hljs-keyword">lambda</span> a, b, c: a + b + c;

<span class="hljs-comment"># Call anonymous sum as a function</span>
print(<span class="hljs-string">"Sum of a, b, c: "</span>, sum( <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span> ))
print(<span class="hljs-string">"Sum of a, b, c: "</span>, sum( <span class="hljs-number">6</span>, <span class="hljs-number">7</span>, <span class="hljs-number">8</span> ))
</code></pre>
<p>In the above example, function definition <strong>sum</strong> expects three inputs a, b, c and returns its summation. When calling <strong>sum</strong> function it appears exactly like any other python function created using <strong>def</strong> with few caveats.</p>
<ul>
<li><p>Lambdas are basically single body expressions and not true function with a statement block. They don't contain any explicit <strong>return</strong> statement rather whatever expression mentioned in lambda is computed and the value returned at the end.</p>
</li>
<li><p>Just like <strong>def</strong> they can take multiple arguments but return just one value.</p>
</li>
<li><p>Lambda functions creates <strong><em>local scope</em></strong> and cannot access variables other than those in their parameter list and those in the global namespace.</p>
</li>
</ul>
<p>Just like def lambda can have default values.</p>
<pre><code class="lang-python">
sum = <span class="hljs-keyword">lambda</span> a=<span class="hljs-number">3</span>, b=<span class="hljs-number">2</span>, c=<span class="hljs-number">1</span> : a + b + c;

print(<span class="hljs-string">"Sum of a, b, c: "</span>, sum( <span class="hljs-number">7</span>, <span class="hljs-number">9</span>, <span class="hljs-number">11</span> ))    <span class="hljs-comment"># prints 27</span>
</code></pre>
<p>We can use lambda to generate multiplier function.</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">multiplier</span>(<span class="hljs-params">n</span>):</span>
  <span class="hljs-keyword">return</span> <span class="hljs-keyword">lambda</span> a : a * n

doubler = multiplier(<span class="hljs-number">2</span>)
print(doubler(<span class="hljs-number">5</span>))        <span class="hljs-comment"># prints 10</span>

tripler = multiplier(<span class="hljs-number">3</span>)
print(tripler(<span class="hljs-number">5</span>))        <span class="hljs-comment"># prints 15</span>
</code></pre>
<p>We can create jump tables.</p>
<pre><code class="lang-python">L = [<span class="hljs-keyword">lambda</span> x: x ** <span class="hljs-number">2</span>, <span class="hljs-keyword">lambda</span> x: x ** <span class="hljs-number">3</span>, <span class="hljs-keyword">lambda</span> x: x ** <span class="hljs-number">4</span>] 

<span class="hljs-keyword">for</span> f <span class="hljs-keyword">in</span> L:
    print(f(<span class="hljs-number">2</span>))    <span class="hljs-comment"># Prints 4, 8, 16</span>

print(L[<span class="hljs-number">0</span>](<span class="hljs-number">3</span>))     <span class="hljs-comment"># Prints 9</span>
</code></pre>
]]></content:encoded></item></channel></rss>