Pewarta Proxy

📋 Server Request & Proxy Logs

Loading active proxy settings...
Developed by Khairul Adha (adhexa.id)
Mengambil log server...

⚙️ Pengaturan Proxy Target

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
bpo-40571: Make lru_cache(maxsize=None) more discoverable
  • Loading branch information
rhettinger committed May 9, 2020
commit b609e34c0820c1eb0df213f9cf2a08e4e8907259
26 changes: 26 additions & 0 deletions Doc/library/functools.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,32 @@ function for the purposes of this module.

The :mod:`functools` module defines the following functions:

.. decorator:: cache(user_function)

Simple lightweight unbounded function cache. Sometimes called
`"memoize" <https://en.wikipedia.org/wiki/Memoization>`_.

Returns the same as ``lru_cache(maxsize=None, typed=False)``, creating a thin
wrapper around a dictionary lookup for the function arguments. Because it
never needs to evict old values, this is smaller and faster than
:func:`lru_cache()` with a size limit.

For example::

@cache
def factorial(n):
return n * fact(n-1) if n else 1

>>> factorial(10) # no previously cached result, makes 11 recursive calls
3628800
>>> factorial(5) # just looks up cached value result
120
>>> factorial(12) # makes two new recursive calls, the other 10 are cached
479001600

.. versionadded:: 3.9


.. decorator:: cached_property(func)

Transform a method of a class into a property whose value is computed once
Expand Down
11 changes: 10 additions & 1 deletion Lib/functools.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
# See C source code for _functools credits/copyright

__all__ = ['update_wrapper', 'wraps', 'WRAPPER_ASSIGNMENTS', 'WRAPPER_UPDATES',
'total_ordering', 'cmp_to_key', 'lru_cache', 'reduce',
'total_ordering', 'cache', 'cmp_to_key', 'lru_cache', 'reduce',
'TopologicalSorter', 'CycleError',
'partial', 'partialmethod', 'singledispatch', 'singledispatchmethod',
'cached_property']
Expand Down Expand Up @@ -888,6 +888,15 @@ def cache_clear():
pass


################################################################################
### cache -- simplified access to the infinity cache
################################################################################

def cache(user_function, /):
Comment thread
rhettinger marked this conversation as resolved.
Resolved
'Simple lightweight unbounded cache. Sometimes called "memoize".'
return lru_cache(maxsize=None, typed=False)(user_function)


################################################################################
### singledispatch() - single-dispatch generic function decorator
################################################################################
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Added functools.cache() as a simpler, more discoverable way to access the
unbounded cache variant of lru_cache(maxsize=None).