lucabaldini / webpage

My personal (static-html) webpage
GNU General Public License v3.0
1 stars 0 forks source link

Use a decorator to memoize the call to the html template construction #3

Closed lucabaldini closed 5 years ago

lucabaldini commented 5 years ago

I created an initial implementation of the decorator in

def memoize(func):
    """Simple decorator to memoize the return value of a function with no
    argument.

    See https://stackoverflow.com/questions/5630409
    for the use of nonlocal in the body of the wrapper function.
    """
    cache = None
    @functools.wraps(func)
    def wrapper():
        nonlocal cache
        if cache is None:
            cache = func()
        return cache
    return wrapper
lucabaldini commented 5 years ago

Done.