diff options
author | Raymond Hettinger <rhettinger@users.noreply.github.com> | 2019-05-26 18:27:35 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-05-26 18:27:35 (GMT) |
commit | b821868e6d909f4805499db519ebc2cdc01cf611 (patch) | |
tree | ae3da900a9fa80bc5098dd4eb3150ebe6e25ac2e /Doc | |
parent | aaf47caf35984e614d93bd8bea5227df55e0e3e6 (diff) | |
download | cpython-b821868e6d909f4805499db519ebc2cdc01cf611.zip cpython-b821868e6d909f4805499db519ebc2cdc01cf611.tar.gz cpython-b821868e6d909f4805499db519ebc2cdc01cf611.tar.bz2 |
bpo-36772 Allow lru_cache to be used as decorator without making a function call (GH-13048)
Diffstat (limited to 'Doc')
-rw-r--r-- | Doc/library/functools.rst | 15 | ||||
-rw-r--r-- | Doc/whatsnew/3.8.rst | 17 |
2 files changed, 31 insertions, 1 deletions
diff --git a/Doc/library/functools.rst b/Doc/library/functools.rst index 16a779f..8b8b1f8 100644 --- a/Doc/library/functools.rst +++ b/Doc/library/functools.rst @@ -76,7 +76,8 @@ The :mod:`functools` module defines the following functions: .. versionadded:: 3.2 -.. decorator:: lru_cache(maxsize=128, typed=False) +.. decorator:: lru_cache(user_function) + lru_cache(maxsize=128, typed=False) Decorator to wrap a function with a memoizing callable that saves up to the *maxsize* most recent calls. It can save time when an expensive or I/O bound @@ -90,6 +91,15 @@ The :mod:`functools` module defines the following functions: differ in their keyword argument order and may have two separate cache entries. + If *user_function* is specified, it must be a callable. This allows the + *lru_cache* decorator to be applied directly to a user function, leaving + the *maxsize* at its default value of 128:: + + @lru_cache + def count_vowels(sentence): + sentence = sentence.casefold() + return sum(sentence.count(vowel) for vowel in 'aeiou') + If *maxsize* is set to ``None``, the LRU feature is disabled and the cache can grow without bound. The LRU feature performs best when *maxsize* is a power-of-two. @@ -165,6 +175,9 @@ The :mod:`functools` module defines the following functions: .. versionchanged:: 3.3 Added the *typed* option. + .. versionchanged:: 3.8 + Added the *user_function* option. + .. decorator:: total_ordering Given a class defining one or more rich comparison ordering methods, this diff --git a/Doc/whatsnew/3.8.rst b/Doc/whatsnew/3.8.rst index a94aba6..1180469 100644 --- a/Doc/whatsnew/3.8.rst +++ b/Doc/whatsnew/3.8.rst @@ -291,6 +291,23 @@ where the DLL is stored (if a full or partial path is used to load the initial DLL) and paths added by :func:`~os.add_dll_directory`. +functools +--------- + +:func:`functools.lru_cache` can now be used as a straight decorator rather +than as a function returning a decorator. So both of these are now supported:: + + @lru_cache + def f(x): + ... + + @lru_cache(maxsize=256) + def f(x): + ... + +(Contributed by Raymond Hettinger in :issue:`36772`.) + + datetime -------- |