Free AI web copilot to create summaries, insights and extended knowledge, download it at here
2313
Abstract
<iframe class="gist-iframe" src="/gist/SongmingLiuForGit/de3be2098974da35958e697eb4fe53c6.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><p id="610a">As you see, the speed just is almost 30x times faster! Now, try to use it in your own Python implementations to boost your programs’ performances!</p><h2 id="8b8b">2. Partial:</h2><p id="6fb4">The <code>partial</code>keyword can help you create a new function out of an existing function by fixing one or more arguments.</p>
<figure id="fe54">
<div>
<div>
<iframe class="gist-iframe" src="/gist/SongmingLiuForGit/c0e2750d99e1e3c2c97e79d3a590ad41.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><p id="c0eb">Consider the example above: here, we have an <code>add</code> function that simply adds two inputs. We can fix <code>a</code> to be 1, and then we get an incrementer! We just need to call <code>partial</code>, pass in our function with fixed argument, we get another new function immediately!</p><p id="15f0">Why this is useful? Well, for certain functions with multiple arguments, some of the arguments are frequently chosen to be a fixed value. Partial just allows us to create a shortcut for those situations easily, just as the counter above!</p><h2 id="054d">3. Reduce</h2><p id="f2af">The reduce function is similar to the reduce word in map-reduce. It takes a stream of data, or in Python language, an iterator, and applies the function to the iterator from left to right. Using reduce keywords allow you to create concise statement for processing iterators without the trouble of doing for loop or while loop.</p>
<figure id="c7cc">
<div>
<div>
<iframe class="gist-iframe" src="/gist/SongmingLiuForGit/3c2647549ea319b5059839bd72e27c6e.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><p id="ca97">The reduce statement could be thought to be equivalent of the follow
Options
ing program according to Python official documents:</p>
<figure id="dc28">
<div>
<div>
<iframe class="gist-iframe" src="/gist/SongmingLiuForGit/64c02ea09b0b37d188078738b480c409.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><p id="80eb">It is now pretty straightforward to see what reduce function does under the hood.</p><h2 id="99d6">4. SingleDispatch</h2><p id="d1b8">Singledispatch is another great tool you can use to practice generic programming. Singledispatch will allow you to define program logic for different types of input, see the programs below:</p>
<figure id="4884">
<div>
<div>
<iframe class="gist-iframe" src="/gist/SongmingLiuForGit/d4bceafc81631a938fd734ea7fdfa813.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><p id="3ec1">The usage of singledispatch is handy, too. First, you define a normal function and do the decorator singledispatch on top of that. Then, you go ahead and define other functions, and you annotate those function with <i>@your_function_name.register(type_of_your_desired _input). </i>Notice that the function you register cannot have the same name as the default function you defined first place; otherwise, your program will throw an error!</p><p id="019e">Those are all the functions I think it is worthwhile for you to learn and use in your Python program. There are some other tools from the functools library you may find useful and handy in practice. Check them out and have fun playing with them. As usual, happy learning and happy coding!</p><p id="1ad3"><i>More content at <a href="https://plainenglish.io/"><b>PlainEnglish.io</b></a>. Sign up for our <a href="http://newsletter.plainenglish.io/"><b>free weekly newsletter</b></a>. Follow us on <a href="https://twitter.com/inPlainEngHQ"><b>Twitter</b></a> and <a href="https://www.linkedin.com/company/inplainenglish/"><b>LinkedIn</b></a>. Join our <a href="https://discord.gg/GtDtUAvyhW"><b>community Discord</b></a>.</i></p></article></body>