Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

wrapper

We created a wrapper function called wrap that receives a function as a parameter.

Inside it creates a new funcion called new_function. (I am really not very creative with names.) The return value of the wrap function is this `new_function.

The new_function first prints something on the screen and saves the current time.

Then it calls the function that was received as a parameter.

Then gets the current time again and prints the elapsed time.

That’s the new_function

On the next pages we’ll see how we can use this function.

import time

def wrap(func):
    def new_function():
        print(f"start new '{func.__name__}'")
        start = time.time()
        func()
        end = time.time()
        print(f"end new '{func.__name__}' {end-start}")
    return new_function