python 2.7的记忆库 [英] memoization library for python 2.7

查看:22
本文介绍了python 2.7的记忆库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到 python 3.2 有 memoization 作为 functools 库中的装饰器.http://docs.python.org/py3k/library/functools.html#functools.lru_cache

I see that python 3.2 has memoization as a decorator in functools library. http://docs.python.org/py3k/library/functools.html#functools.lru_cache

不幸的是,它还没有向后移植到 2.7.是否有任何特定原因导致它在 2.7 中不可用?是否有任何 3rd 方库提供相同的功能,还是我应该自己编写?

Unfortunately it is not yet backported to 2.7. Is there any specific reason as why it is not available in 2.7? Is there any 3rd party library providing the same feature or should I write my own?

推荐答案

是否有任何具体原因导致它在 2.7 中不可用?

Is there any specific reason as why it is not available in 2.7?

@Nirk 已经提供了原因:不幸的是,2.x 线只收到错误修正,新功能是仅针对 3.x 开发.

@Nirk has already provided the reason: unfortunately, the 2.x line only receive bugfixes, and new features are developed for 3.x only.

是否有任何第三方库提供相同的功能?

Is there any 3rd party library providing the same feature?

repoze.lru 是一个 LRU 缓存实现适用于 Python 2.6、Python 2.7 和 Python 3.2.

repoze.lru is a LRU cache implementation for Python 2.6, Python 2.7 and Python 3.2.

文档和源代码可在 GitHub 上找到.

Documentation and source code are available on GitHub.

简单用法:

from repoze.lru import lru_cache

@lru_cache(maxsize=500)
def fib(n):
    if n < 2:
        return n
    return fib(n-1) + fib(n-2)

这篇关于python 2.7的记忆库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆