如何配置lru_cache
以根据实际接收到的值而不是函数调用方式来键入其缓存?
>>> from functools import lru_cache
>>> @lru_cache
... def f(x=2):
... print("reticulating splines...")
... return x ** 2
...
>>> f()
reticulating splines...
4
>>> f(2)
reticulating splines...
4
>>> f(x=2)
reticulating splines...
4
换句话说,只有上述第一次调用应该是缓存未命中,其他两次调用应该是缓存命中。
f.cache_info()
方法来查看实际的缓存命中/未命中情况。 - bnaeckermake_key
并不知道。 - user2357112f()
和f(x=2)
不被视为相同?在这两种情况下,args=()
和kwds={'x': 2}
难道不是一样的吗? - mkrieger1