decorators logo decorators: args + kwargs

1 2 3 4 5 6 7 8 9 10

If you're not familiar with args/kwargs you should check out the course on this topic first here. Assuming you're familiar, we'll now make the functionality-adding function from the previous video better.

import time
import random

def stopwatch(f):
    def func(*args, **kwargs):
        tic = time.time()
        result = f(*args, **kwargs)
        print(f"this function took: {time.time() - tic}")
        return result
    return func

def sleep_random(s):
    t = s + random.random()
    return "Done"

timed_sleep = stopwatch(sleep_random)

Again, now have two functions that are similar, but they now both allow for an input.