python - 无法进入lru_cache'_lru_cache_wrapper

标签 python lru functools

出于某种原因,我根本无法进入 _lru_cache_wrapper 的代码。

我看到 lru_cache 装饰器在 functools.py 中返回一个带有 _lru_cache_wrapper 的闭包。但是,当我在 _lru_cache_wrapper 的代码中放置断点时,它永远不会被触发。我什至放置了一个 print() 语句,但它似乎没有被击中。我对此感到非常困惑,因为 lru_cache 代码命中了断点,但没有命中 _lru_cache_wrapper。

from functools import lru_cache

@lru_cache()
def foo():
    print('foo')

if __name__ == '__main__':
    foo()
    print(foo.cache_info())

最佳答案

functools.py 中的版本未使用。这是replaced_functools 中用 C 编写的版本:

try:
    from _functools import _lru_cache_wrapper
except ImportError:
    pass

关于python - 无法进入lru_cache'_lru_cache_wrapper,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54186904/

相关文章:

python - 为什么要在 Python 中设置部分对象的 __doc__?

python-3.x - 如何将可调用对象作为参数传递给 `functools.partial`

python - 为什么 functools.lru_cache 会破坏这个功能?

python - python中如何通过折叠实现Unicode字符串匹配

python - 在python中,如何检查列表列表是否有重复项?

python - 将通配符应用于 Pandas isin 过滤器

python - Lru_cache(来自 functools)如何工作?

python - 使用 PyPy 或 Shedskin 加速现有 Python 应用程序的一部分

memcached - memcached中的惰性过期机制是如何运作的?