5 python features I wish I had known earlier
Tags: python, pythonfeatures, programming
Published: 2020-04-28 05:58:49

<p><span style="font-weight: 400;">There are several helpful concepts in Python that I knew about from the origin: such as generators, iterators, context managers, and so on, but there are many features that are not obvious until you met them. I essentially use Python 2.6-2.7 for writing small cross-platform (including windows) text processing console utilities, and here are several of my favorites.</span></p>
<ol>
<li><strong> MEMORIZATION:</strong></li>
</ol>
<p><span style="font-weight: 400;">One can take the guidance of the functional programming technique in Python and build memorizers [like the one below], which could probably take any recursive function as an input and return a memoized version of it, which makes the code to run significantly quicker.</span></p>
<ol>
<li style="font-weight: 400;"><strong>def</strong><span style="font-weight: 400;"> memoize</span><span style="font-weight: 400;">(</span><span style="font-weight: 400;">f</span><span style="font-weight: 400;">):</span></li>
<li style="font-weight: 400;"><span style="font-weight: 400;">&nbsp;&nbsp;&nbsp;</span><strong>def</strong><span style="font-weight: 400;"> memf</span><span style="font-weight: 400;">(*</span><span style="font-weight: 400;">x</span><span style="font-weight: 400;">):</span></li>
<li style="font-weight: 400;"><span style="font-weight: 400;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><strong>if</strong><span style="font-weight: 400;"> x </span><strong>not</strong> <strong>in</strong><span style="font-weight: 400;"> memf</span><span style="font-weight: 400;">.</span><span style="font-weight: 400;">cache</span><span style="font-weight: 400;">:</span></li>
<li style="font-weight: 400;"><span style="font-weight: 400;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;memf</span><span style="font-weight: 400;">.</span><span style="font-weight: 400;">cache</span><span style="font-weight: 400;">[</span><span style="font-weight: 400;">x</span><span style="font-weight: 400;">]</span> <span style="font-weight: 400;">=</span><span style="font-weight: 400;"> f</span><span style="font-weight: 400;">(*</span><span style="font-weight: 400;">x</span><span style="font-weight: 400;">)</span></li>
<li style="font-weight: 400;"><span style="font-weight: 400;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><strong>return</strong><span style="font-weight: 400;"> memf</span><span style="font-weight: 400;">.</span><span style="font-weight: 400;">cache</span><span style="font-weight: 400;">[</span><span style="font-weight: 400;">x</span><span style="font-weight: 400;">]</span></li>
<li style="font-weight: 400;"><span style="font-weight: 400;">&nbsp;&nbsp;&nbsp;memf</span><span style="font-weight: 400;">.</span><span style="font-weight: 400;">cache </span><span style="font-weight: 400;">=</span> <span style="font-weight: 400;">{}</span></li>
<li style="font-weight: 400;"><span style="font-weight: 400;">&nbsp;&nbsp;&nbsp;</span><strong>return</strong><span style="font-weight: 400;"> memf</span></li>
</ol>
<p><span style="font-weight: 400;">This can be called as follows:</span></p>
<ol>
<li style="font-weight: 400;"><span style="font-weight: 400;">recursive_function </span><span style="font-weight: 400;">=</span><span style="font-weight: 400;"> memoize</span><span style="font-weight: 400;">(</span><span style="font-weight: 400;">recursive_function</span><span style="font-weight: 400;">)</span></li>
</ol>
<p><span style="font-weight: 400;">And viola, the recursive_function is now memorized.</span></p>
<p>&nbsp;</p>
<ol start="2">
<li><a href="https://techvidvan.com/tutorials/python-list-comprehension/"><strong>LIST COMPREHENSIONS</strong></a><strong>:</strong></li>
</ol>
<p>&nbsp;</p>
<ol>
<li style="font-weight: 400;"><span style="font-weight: 400;">lst </span><span style="font-weight: 400;">=</span> <span style="font-weight: 400;">[</span><span style="font-weight: 400;"> i</span><span style="font-weight: 400;">*</span><span style="font-weight: 400;">i </span><strong>for</strong><span style="font-weight: 400;"> i </span><strong>in</strong><span style="font-weight: 400;"> xrange</span><span style="font-weight: 400;">(</span><span style="font-weight: 400;">10</span><span style="font-weight: 400;">)]</span></li>
</ol>
<p><span style="font-weight: 400;">Compare this with the one below</span></p>
<ol>
<li style="font-weight: 400;"><span style="font-weight: 400;">lst </span><span style="font-weight: 400;">=</span> <span style="font-weight: 400;">[]</span></li>
<li style="font-weight: 400;"><strong>for</strong><span style="font-weight: 400;"> i </span><strong>in</strong><span style="font-weight: 400;"> xrange</span><span style="font-weight: 400;">(</span><span style="font-weight: 400;">10</span><span style="font-weight: 400;">):</span></li>
<li style="font-weight: 400;"><span style="font-weight: 400;">lst</span><span style="font-weight: 400;">.</span><span style="font-weight: 400;">append</span><span style="font-weight: 400;">(</span><span style="font-weight: 400;">i</span><span style="font-weight: 400;">*</span><span style="font-weight: 400;">i</span><span style="font-weight: 400;">)</span></li>
</ol>
<p>&nbsp;</p>
<ol start="3">
<li><a href="https://data-flair.training/blogs/python-lambda-expression/"><strong>LAMBDAS, MAP, REDUCE AND FILTERS</strong></a><strong>:</strong></li>
</ol>
<p><span style="font-weight: 400;">These features add to the richness of the language.</span></p>
<ol>
<li style="font-weight: 400;"><span style="font-weight: 400;">square </span><span style="font-weight: 400;">=</span> <strong>lambda</strong><span style="font-weight: 400;"> x</span><span style="font-weight: 400;">:</span><span style="font-weight: 400;">x</span><span style="font-weight: 400;">**</span><span style="font-weight: 400;">2</span></li>
<li style="font-weight: 400;"><span style="font-weight: 400;">map</span><span style="font-weight: 400;">(</span><span style="font-weight: 400;">square</span><span style="font-weight: 400;">,[</span><span style="font-weight: 400;">1</span><span style="font-weight: 400;">,</span><span style="font-weight: 400;">2</span><span style="font-weight: 400;">,</span><span style="font-weight: 400;">3</span><span style="font-weight: 400;">])</span> <span style="font-weight: 400;">## returns [1, 4, 9]</span></li>
<li style="font-weight: 400;"><span style="font-weight: 400;">is_divisible_by_3 </span><span style="font-weight: 400;">=</span> <strong>lambda</strong><span style="font-weight: 400;"> x</span><span style="font-weight: 400;">:</span><span style="font-weight: 400;">x</span><span style="font-weight: 400;">%</span><span style="font-weight: 400;">3</span><span style="font-weight: 400;">==</span><span style="font-weight: 400;">0</span></li>
<li style="font-weight: 400;"><span style="font-weight: 400;">filter</span><span style="font-weight: 400;">(</span><span style="font-weight: 400;">is_divisible_by_3</span><span style="font-weight: 400;">,[</span><span style="font-weight: 400;">1</span><span style="font-weight: 400;">,</span><span style="font-weight: 400;">2</span><span style="font-weight: 400;">,</span><span style="font-weight: 400;">3</span><span style="font-weight: 400;">,</span><span style="font-weight: 400;">4</span><span style="font-weight: 400;">,</span><span style="font-weight: 400;">5</span><span style="font-weight: 400;">,</span><span style="font-weight: 400;">6</span><span style="font-weight: 400;">])</span> <span style="font-weight: 400;">## returns [3,6]</span></li>
<li style="font-weight: 400;"><span style="font-weight: 400;">reduce</span><span style="font-weight: 400;">(</span><span style="font-weight: 400;">min</span><span style="font-weight: 400;">,[</span><span style="font-weight: 400;">1</span><span style="font-weight: 400;">,</span><span style="font-weight: 400;">2</span><span style="font-weight: 400;">,</span><span style="font-weight: 400;">3</span><span style="font-weight: 400;">,</span><span style="font-weight: 400;">4</span><span style="font-weight: 400;">,</span><span style="font-weight: 400;">5</span><span style="font-weight: 400;">,</span><span style="font-weight: 400;">6</span><span style="font-weight: 400;">])</span> <span style="font-weight: 400;">## returns 1, you can as well use min([1,2,3,4])</span></li>
</ol>
<p>&nbsp;</p>
<ol start="4">
<li><a href="https://techvidvan.com/tutorials/python-generators/"><strong> GENERATORS</strong></a><strong>:</strong></li>
</ol>
<p><span style="font-weight: 400;">Ever required to iterate over a very wide collection of items like your linux file-system tree. Generators make life more comfortable for you.</span></p>
<p><span style="font-weight: 400;">This is an easy code snippet that uses walk function from the os module and prints out all files and directors available starting from the root directory. os.walk() is a generator function which supplies the following item to be iterated upon freezes its context and goes into a suspended state, the next time when the next iteration is to be performed it supplies the next item and goes back into the frozen state again until when all items are utilized, in which instance it returns a StopIteration exception.</span></p>
<ol>
<li style="font-weight: 400;"><span style="font-weight: 400;">path </span><span style="font-weight: 400;">=</span> <span style="font-weight: 400;">"/home"</span></li>
<li style="font-weight: 400;"><strong>for</strong><span style="font-weight: 400;"> files os</span><span style="font-weight: 400;">.</span><span style="font-weight: 400;">walk</span><span style="font-weight: 400;">(</span><span style="font-weight: 400;">path</span><span style="font-weight: 400;">):</span></li>
<li style="font-weight: 400;"><span style="font-weight: 400;">&nbsp;&nbsp;&nbsp;&nbsp;</span><strong>print</strong><span style="font-weight: 400;"> files</span></li>
</ol>
<p>&nbsp;</p>
<ol start="5">
<li><a href="https://data-flair.training/blogs/python-debugger/"><strong>DEBUGGER</strong></a><strong>:</strong></li>
</ol>
<p><span style="font-weight: 400;">Python gives a pdb module, an simple to use, and extremely helpful debugger.</span></p>
<p><span style="font-weight: 400;">Just type </span><strong>import pdb; pdb.set_trace() </strong><span style="font-weight: 400;">to set a breakpoint wherever in your code.</span></p>
<p><span style="font-weight: 400;">Explore more </span><a href="https://data-flair.training/blogs/python-projects-with-source-code/"><span style="font-weight: 400;">python projects</span></a><span style="font-weight: 400;"> and get your concepts clear.&nbsp;</span></p>