lru版本python
时间: 2023-09-15 22:20:52 浏览: 111
lru_dict的python库
The `lru_cache` decorator is a part of the standard library in Python starting from version 3.2. It provides a simple way to implement memoization or caching functionality for function calls.
If you're using Python 3.2 or later, you can import and use `lru_cache` directly without the need to install any additional libraries. Here's an example:
```python
from functools import lru_cache
@lru_cache(maxsize=None)
def fibonacci(n):
if n <= 1:
return n
return fibonacci(n-1) + fibonacci(n-2)
```
In the above example, the `lru_cache` decorator is applied to the `fibonacci` function, enabling caching for its results.
However, if you're using an older version of Python (earlier than 3.2), the `lru_cache` decorator is not available in the standard library. In that case, you can use third-party libraries like `functools32` or `repoze.lru` to achieve similar functionality.
To install `functools32`, you can use pip:
```
pip install functools32
```
After installing `functools32`, you can import and use `lru_cache` as shown in the example above.
Please note that it is generally recommended to use the version of Python that is actively supported and regularly updated to benefit from the latest features and bug fixes.
阅读全文