From fd8366872e58a37b79e0b4733b556f519e581ae4 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Fri, 4 Jun 2021 20:54:10 -0700 Subject: [PATCH 1/5] Simplify the count_vowels example --- Doc/library/functools.rst | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Doc/library/functools.rst b/Doc/library/functools.rst index e981bcdf6f2573..b157b84cca4494 100644 --- a/Doc/library/functools.rst +++ b/Doc/library/functools.rst @@ -154,8 +154,7 @@ The :mod:`functools` module defines the following functions: @lru_cache def count_vowels(sentence): - sentence = sentence.casefold() - return sum(sentence.count(vowel) for vowel in 'aeiou') + return sum(sentence.count(vowel) for vowel in 'AEIOUaeiou') If *maxsize* is set to ``None``, the LRU feature is disabled and the cache can grow without bound. From b4ef8950f08e88a63e9bb7eb3d42c68e91d85ce5 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Fri, 4 Jun 2021 21:02:05 -0700 Subject: [PATCH 2/5] Hits and misses are fetched while a lock is held --- Doc/library/functools.rst | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Doc/library/functools.rst b/Doc/library/functools.rst index b157b84cca4494..094a94c126205c 100644 --- a/Doc/library/functools.rst +++ b/Doc/library/functools.rst @@ -171,8 +171,7 @@ The :mod:`functools` module defines the following functions: To help measure the effectiveness of the cache and tune the *maxsize* parameter, the wrapped function is instrumented with a :func:`cache_info` function that returns a :term:`named tuple` showing *hits*, *misses*, - *maxsize* and *currsize*. In a multi-threaded environment, the hits - and misses are approximate. + *maxsize* and *currsize*. The decorator also provides a :func:`cache_clear` function for clearing or invalidating the cache. From 61b527f5bd15e7b24ca42ce30a27e2e16f0724c5 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Sat, 5 Jun 2021 13:40:08 -0700 Subject: [PATCH 3/5] Add note that references are kept for arguments and return values --- Doc/library/functools.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Doc/library/functools.rst b/Doc/library/functools.rst index 094a94c126205c..6161140fde9f48 100644 --- a/Doc/library/functools.rst +++ b/Doc/library/functools.rst @@ -180,6 +180,9 @@ The :mod:`functools` module defines the following functions: :attr:`__wrapped__` attribute. This is useful for introspection, for bypassing the cache, or for rewrapping the function with a different cache. + The cache keeps references to the arguments and return values until they age + out of cache or until the cache is cleared. + An `LRU (least recently used) cache `_ works best when the most recent calls are the best predictors of upcoming From 77c826a75216d958cf4214c5c9deab49a9feb04b Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Sat, 5 Jun 2021 13:41:11 -0700 Subject: [PATCH 4/5] Fix typo --- Doc/library/functools.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/functools.rst b/Doc/library/functools.rst index 6161140fde9f48..90a5284f549649 100644 --- a/Doc/library/functools.rst +++ b/Doc/library/functools.rst @@ -181,7 +181,7 @@ The :mod:`functools` module defines the following functions: bypassing the cache, or for rewrapping the function with a different cache. The cache keeps references to the arguments and return values until they age - out of cache or until the cache is cleared. + out of the cache or until the cache is cleared. An `LRU (least recently used) cache `_ From 094c41ac8cc7062f3c3205437ffbb52b72c5826e Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Sat, 5 Jun 2021 15:39:58 -0700 Subject: [PATCH 5/5] Clarify behavior when *typed* is false. --- Doc/library/functools.rst | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Doc/library/functools.rst b/Doc/library/functools.rst index 90a5284f549649..871c94afaf99ec 100644 --- a/Doc/library/functools.rst +++ b/Doc/library/functools.rst @@ -160,8 +160,10 @@ The :mod:`functools` module defines the following functions: grow without bound. If *typed* is set to true, function arguments of different types will be - cached separately. For example, ``f(3)`` and ``f(3.0)`` will be treated - as distinct calls with distinct results. + cached separately. For example, ``f(3)`` and ``f(3.0)`` will always be + treated as distinct calls with distinct results. If *typed* is false, + the implementation will usually but not always regard them as equivalent + calls and only cache a single result. The wrapped function is instrumented with a :func:`cache_parameters` function that returns a new :class:`dict` showing the values for *maxsize*