May 2018
Intermediate to advanced
380 pages
9h 37m
English
A common use of decorators, outside of frameworks, is to memoize functions. Memoization caches the results of a function call to a dictionary; if the function is called again with the same arguments, the result is pulled from the cache rather than rerunning the function again. Many memoization functions and decorators have been created, but most don't preserve the signature. The following examples are taken from the decorator module's documentation (http://decorator.readthedocs.io/en/latest/tests.documentation.html):
import functools import time def memoize_uw(func): func.cache = {} def memoize(*args, **kw): if kw: # frozenset ...