Questions tagged [python-lru-cache]
11 questions
2
votes
1 answer
TypeVar inference broken by lru_cache decorator
python's TypeVar inference broken when using lru_cache decorator. For example, after applying mypy the following example, only function with lru_cache causes error like:
main.py:14: error: Incompatible types in assignment (expression has type "T",…

orematasaburo
- 1,207
- 10
- 20
1
vote
2 answers
What is faster in Python, generating a uuid, using LRU cache or retrieve from a dict
I need to generate around a few hundreds of UUIDs and then I need to reuse each UUID a few thousands times.
What will give me better performance?
Option 1: Generate the uuid every time from the input?
Option 2: Use Python's lru_cache(maxsize=None)…

RaamEE
- 3,017
- 4
- 33
- 53
1
vote
2 answers
Python get the cache dictionary of _lru_cache_wrapper object
I have this simple cache decorator demo here
@functools.cache
def cached_fib(n):
assert n > 0
if n <= 2:
return 1
return cached_fib(n - 1) + cached_fib(n - 2)
t1 = time.perf_counter()
cached_fib(400)
t2 =…

Fed_Dragon
- 1,048
- 1
- 3
- 15
0
votes
0 answers
Python caching results of large computations (with a twist)
I'm good with Python and use functions to process large datasets from files, then return the results. I've realized these input files and other parameters don't change often, so I could store the returned results for reuse.
I've used…

Gilma
- 1
- 2
0
votes
2 answers
TypeError when using lru_cache with class inheritance in Python 3.8
Description: I am trying to use the lru_cache decorator from the functools module to cache instances of two classes, A and B, where B inherits from A. Here is my code:
from functools import lru_cache
@lru_cache(1)
class A:
def __init__(self):
…

Shawn
- 3
- 2
0
votes
1 answer
How to use lru_cache for static method and with unhashable arguments like lists in python
How to use lru_cache for static method and with unhashable arguments like lists in python
I have tried using methodtools lru_cache method. It gives an error that call is not working.
0
votes
0 answers
Python lru_cache nested decorator
I want to basically ignore a dict parameter in a method that want to decorate with functools.lru_cache.
This is the code I have written so far:
import functools
class BlackBox:
"""All BlackBoxes are the same."""
def __init__(self,…

Sanandrea
- 2,112
- 1
- 27
- 45
0
votes
0 answers
using %callgraph together with lru_cache without decorator
Setup (on Notebook / Colab)
# Install
!pip install callgraph
# We will also use
from functools import lru_cache
%load_ext callgraph
In Python 3.x , if I do
@lru_cache(maxsize=128)
def factorial(n):
return n * factorial(n-1) if n else…

Rub
- 2,071
- 21
- 37
0
votes
0 answers
C++ LRUCache decorator | call a non-member function in a class scope
You may be familiar with Python decorators such as @lru_cache. It wraps any function and does MEMOIZATION of results improving the runtime:
@functools.lru_cache(maxsize=100)
def fib(n):
if n < 2:
return n
return fib(n-1) +…

user2376997
- 501
- 6
- 22
0
votes
1 answer
Does python's functools lru_cache caches function parameters alongside the result?
For the following program:
from functools import lru_cache
@lru_cache(maxsize=256)
def task_a(a, b):
print(f'Multiplying {a} and {b}')
return a*b
print(task_a(2, 3))
print(task_a(2, 3))
print(task_a(2, 4))
print(task_a(2,…

Deepak Tatyaji Ahire
- 4,883
- 2
- 13
- 35
-3
votes
0 answers
Python LRU cache class error. Required positional argument
I have task to use decorators in class methods. Without them it worked but now it require positional argument at object initialization. What's my mistake?
from collections import OrderedDict
class LRUCache:
def __init__(self, capacity: int):
…

Alexander
- 5
- 3