简体   繁体   English

python @memoize vs functools.lru_cache

[英]python @memoize vs functools.lru_cache

I'm not sure what the advantages and disadvantages of these 2 are.我不确定这两个的优缺点是什么。 Provided @lru_cache from functools is a standard library tool and seems to have more control over @memoize , why does @memoize even exist?如果来自 functools 的@lru_cache是一个标准库工具,并且似乎对@memoize有更多控制权,为什么@memoize甚至存在?

Edit: I am referring to memoize from here -> https://wiki.python.org/moin/PythonDecoratorLibrary#Memoize编辑:我指的是这里的备忘录 -> https://wiki.python.org/moin/PythonDecoratorLibrary#Memoize

functools.lru_cache was added in python 3.2. functools.lru_cache是在 python 3.2 中添加的。 For those using python 2, and for libraries written to work with it (eg all major libs) it cannot be used.对于那些使用 python 2 的人,以及为使用它而编写的库(例如所有主要库),它不能使用。

The workaround is to use the @memoize recipe from the decorator library.解决方法是使用装饰器库中的@memoize配方。

If you only use python 3, there is basically no reason not to use lru_cache .如果只使用 python 3,基本上没有理由不使用lru_cache

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM