4 ms·
> Don’t use lru_cache I disagree- it works wonders for expensive functions that return strings and ints. Maybe "don't use lru_cache with mutable values" would
by Xavdidtheshadow 5y ago
> Don’t use lru_cache
I disagree- it works wonders for expensive functions that return strings and ints. Maybe "don't use lru_cache with mutable values" would be more accurate.
Thanks for noting that edge case though, I've never thought about that.
- matsemann 5y agoKinda the same as using mutable values (list and dicts maybe being the most common pitfalls) as default arguments to functions. Shared between all function calls.
- pyuser583 5y agoThis behavior might be justifiable - if it were included in the docs. If you want to reduce resource consumption, you could zip the results, or deduplicate the values using a separate dict. There’s nothing unPythonic about a function returning mutable values. A common use for caching is wrapping a call to a remote servicer. If the response is JSON, it’s likely to be mutable. Nothing weird or edgy about that.