12.01.21 12:02, Steven D'Aprano пише:
> Is anyone else interested in additional introspection facilities for the 
> functools.lru_cache?
> 
> You can view the cache hit and miss statistics using the cache_info() 
> method, but you can't see what values actually are in the cache. That 
> would be useful to me.
> 
> I propose a method:
> 
>     @functools.lru_cache()
>     def function(arg):
>         ...
> 
>     function.cache_export()
> 
> that returns a dictionary {arg: value} representing the cache. It 
> wouldn't be the cache itself, just a shallow copy of the cache data.

What if the function supports multiple arguments (including passed by
keyword)? Note that internal representation of the key is an
implementation detail, so you need to invent and specify some new
representation. For example return a list of tuples (args, kwargs, result).

Depending on the implementation, getting the list of all arguments can
have larger that linear complexity.

Other cache implementations can contain additional information: the
number of hits for every value, times. Are you interesting to get that
information too or ignore it?

Currently the cache is thread-safe in CPython, but getting all arguments
and values may not be (or we will need to add a synchronization overhead
for every call of the cached function).

And finally, what is your use case? Is it important enough to justify
the cost?
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/IUOWLRO425FVYOA7ICRCTPYQCUBELCWB/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to