Summary
Free AI web copilot to create summaries, insights and extended knowledge, download it at here
3055
Abstract
p id="de88">So the above apply function could become:</p><div id="e937"><pre><span class="hljs-attribute">for</span> i in map(abs,[<span class="hljs-number">1</span>, -<span class="hljs-number">2</span>, -<span class="hljs-number">5</span>, <span class="hljs-number">6</span>.<span class="hljs-number">2</span>]):
<span class="hljs-attribute">print</span>(i)</pre></div><div id="8b55"><pre><span class="hljs-comment"># Output:</span>
<span class="hljs-attribute">1</span>
<span class="hljs-attribute">2</span>
<span class="hljs-attribute">5</span>
<span class="hljs-attribute">6</span>.<span class="hljs-number">2</span></pre></div><p id="b7f1">To read more about the map operation, head over to: <a href="https://www.geeksforgeeks.org/python-map-function/">Python map</a></p><h1 id="4cab">Functions as elements of a list</h1><p id="601f">Functions can be stored as elements of a list or any other data structure in Python. For example, if we wish to perform multiple operations to a particular number, we define <code>apply_func(L, x)</code> that takes a list of functions, <i>L</i> and an operand, <i>x </i>and applies each function in <i>L </i>on <i>x</i>.</p>
<figure id="ea51">
<div>
<div>
<iframe class="gist-iframe" src="/gist/ChaitanyaBaweja/e645f9620701c5d287ff8da0fc813ad3.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><p id="13ad">Now, to call this function:</p><div id="7e6f"><pre>function_list = [<span class="hljs-built_in">abs</span>,<span class="hljs-built_in">exp</span>,<span class="hljs-built_in">int</span>]</pre></div><div id="6a40"><pre><span class="hljs-attribute">print</span> apply_func(function_list,-<span class="hljs-number">2</span>.<span class="hljs-number">3</span>) #<span class="hljs-meta"> [2.3, 0.10025884, -2]</span>
<span class="hljs-comment"># Functions in function_list are applied to -2.3</span></pre></div><div id="7791"><pre><span class="hljs-attribute">print</span> apply_func(function_list,-<span class="hljs-number">4</span>.<span class="hljs-number">6</span>) #<span class="hljs-meta"> [4.6, 0.01005184, -4]</span></pre></div><p id="4658"><b>Side Note:</b> In both the above examples, we could have used list comprehensions. <a href="https://readmedium.com/https-medium-com-chaitanyabaweja1-list-comprehensions-in-python-97786ffc9338"><b>List comprehensions</b></a> in python provide an elegant way of creating lists based on another list or iterator. For example, lines <i>14-16</i> in <code>apply_func</code> could become:</p><div id="116a"><pre><span class="hljs-attribute">output</span> <span class="hljs-operator">=</span> [f(x) for f in L]</pre></div><h1 id="9a55">Functions can be assigned to other variables</h1><p id="8169">An interesting point to note here is that we can assign different variables for these functions. This means, we can say:</p><div id="9b6a"><pre>i = <span class="hljs-built_in">abs</span>
Options
<span class="hljs-keyword">print</span>(<span class="hljs-built_in">i</span>(<span class="hljs-number">-2</span>)) <span class="hljs-meta"># 2</span></pre></div><p id="a851">This is because functions operate exactly like objects and can be passed around. But, an idea that is even more powerful is that <b>all objects can behave like functions as well</b>.</p><h1 id="2ace">Objects as functions</h1><p id="3f29">This part will require us to understand a little bit more about what makes an object a <i>function</i>. Objects can be treated as functions means that they can be called by using round paranthesis <code>()</code>, just like a function.</p><p id="5ca1">This can be done by defining a <i>call</i> function inside the object. Let’s define a class that when called, prints whatever it stores.</p>
<figure id="9b52">
<div>
<div>
<iframe class="gist-iframe" src="/gist/ChaitanyaBaweja/ccf5df8beceffcb9f2db571e566cc9c3.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><div id="0515"><pre>s1 = Printer(<span class="hljs-string">'Hello'</span>) # Defining object of <span class="hljs-keyword">class</span> <span class="hljs-symbol">Printer</span>
<span class="hljs-symbol">Calling</span> <span class="hljs-symbol">object</span> <span class="hljs-symbol">s1</span>
<span class="hljs-symbol">s1</span>() # <span class="hljs-symbol">Hello</span> </pre></div><p id="f3f4">Thus, under the hood <i>‘calling’</i> an object means executing the object’s <i>call</i> method.</p><h1 id="0e67">Key Takeaways</h1><ul><li>Functions in python are first class objects. This means that they can be passed as arguments to other functions, assigned to variables or even stored as elements in various data structures.</li><li>The ability to perform the same operation on a list of elements is provided by a higher-order python function called <b>map</b>.</li><li>Objects can be made callable by defining a <i>call</i> function.</li></ul><p id="6ea0">You can read more about this topic at:</p><ul><li><a href="https://dbader.org/blog/python-first-class-functions">https://dbader.org/blog/python-first-class-functions</a></li><li><a href="https://www.edx.org/course/introduction-to-computer-science-and-programming-using-python-0">https://www.edx.org/course/introduction-to-computer-science-and-programming-using-python-0</a></li><li><a href="https://stackoverflow.com/questions/25292239/are-functions-objects-in-python">https://stackoverflow.com/questions/25292239/are-functions-objects-in-python</a></li></ul><p id="a6dd">If you liked this, you might find <a href="https://readmedium.com/list-comprehensions-in-python-e8d409bb216e"><b>List Comprehensions in Python</b></a><b> </b>and<b> <a href="https://readmedium.com/https-medium-com-python-pandemonium-an-introduction-to-python-sets-part-i-120974a713be">An Introduction to Python Sets</a></b> useful.</p></article></body>