Use functools' @lru_cache without specifying maxsize parameter

后端 未结 3 844
遇见更好的自我
遇见更好的自我 2021-01-04 03:56

The documentation for lru_cache gives the function definition:

@functools.lru_cache(maxsize=128, typed=False)

Th

3条回答
  •  自闭症患者
    2021-01-04 04:21

    On Python 3.8+ you can use @lru_cache without parentheses, so your code snippet will work

    Python 3.8.0 (default, Oct 28 2019, 16:14:01) 
    [GCC 9.2.1 20191008] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import functools
    >>> @functools.lru_cache
    ... def f():
    ...     return 2
    ... 
    >>> 
    

    This was added on May 26, 2019 in commit b821868e.

    On Python 3.7 or below you have to do @lru_cache(). As in, add parentheses after @lru_cache

提交回复
热议问题