diff --git a/Lib/functools.py b/Lib/functools.py --- a/Lib/functools.py +++ b/Lib/functools.py @@ -121,12 +121,15 @@ _CacheInfo = namedtuple("CacheInfo", "hits misses maxsize currsize") -def lru_cache(maxsize=100): +def lru_cache(maxsize=100, typed=False): """Least-recently-used cache decorator. If *maxsize* is set to None, the LRU features are disabled and the cache can grow without bound. + If *typed* is True, arguments of different types will be cached separately. + For example, f(3.0) and f(3) will be treated as distinct calls. + Arguments to the cached function must be hashable. View the cache statistics named tuple (hits, misses, maxsize, currsize) with @@ -157,6 +160,8 @@ key = args if kwds: key += kwd_mark + tuple(sorted(kwds.items())) + if typed: + key += tuple(map(type, key)) try: result = cache[key] hits += 1 @@ -178,6 +183,8 @@ key = args if kwds: key += kwd_mark + tuple(sorted(kwds.items())) + if typed: + key += tuple(map(type, key)) with lock: try: result = cache[key]