avatarTech & Math

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>

4 Must-Know Decorators from the Functools Library in Python

Learn functools to boost your Python program performance

credit due to Ameen Fahmy

In Python3, the high-order function library functools was introduced. Learning how to use it in the correct way can enhance your skills in Python and make your Python program more efficient. In this article, you will learn 4 most useful tools from functools: 1.lru_cache, 2.partial 3.reduce 4.singledispatch. You will be able to understand why we use them and how we use them in a detailed way.

1.lru_cache:

LRU cache refers to the least recently used cache. A cache is a system that remembers the calculation result from history and is able to save time by returning memorized results directly rather than re-calculating. A classical but illustrative example is to calculate the nth number of the Fibonacci sequence. Consider the following program:

Here, we first used the pre-optimized recursion method to calculate the 20th Fibonacci number. By using the timer function of Python, we see our program finishes 0.0036 seconds. (Time may vary on your computer). One aspect to notice is that we calculate some things repetitively. For instance, to calculate the 5th Fibonacci number, we have to calculate the 3rd Fibonacci at least twice. Rather than re-calculating them, we can just remember the 3rd Fibonacci number, and return it immediately for next time. This is basically what lru_cache enables you to do. To use it, simply add @lru_cache on top of your function, and you are all set!

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!

2. Partial:

The partialkeyword can help you create a new function out of an existing function by fixing one or more arguments.

Consider the example above: here, we have an add function that simply adds two inputs. We can fix a to be 1, and then we get an incrementer! We just need to call partial, pass in our function with fixed argument, we get another new function immediately!

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!

3. Reduce

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.

The reduce statement could be thought to be equivalent of the following program according to Python official documents:

It is now pretty straightforward to see what reduce function does under the hood.

4. SingleDispatch

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:

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 @your_function_name.register(type_of_your_desired _input). 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!

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!

More content at PlainEnglish.io. Sign up for our free weekly newsletter. Follow us on Twitter and LinkedIn. Join our community Discord.

Python
Python3
Programming
Software Development
Coding
Recommended from ReadMedium