-
-
Notifications
You must be signed in to change notification settings - Fork 33.3k
Description
Documentation
Currently the documentation for functools.cached_property recommends stacking @property and @functools.cache decorators to "achieve an effect similar to" functools.cached_property. But this omits a critical problem with using @functools.cache on instance methods: it is an unbounded cache, and will use self as a cache key, so any instance on which this "cached property" is ever accessed will be kept alive by the cache forever (unless the cache is manually cleared), and the memory used by these cached instances can grow unboundedly.
The FAQ entry on caching methods does not recommend (or even mention) functools.cache, and it explicitly recommends a bounded functools.lru_cache in order to avoid keeping alive all instances indefinitely.
Specific proposals:
- The
cached_propertydocumentation should not recommend usingfunctools.cacheon a method. - It also doesn't need to give a full example of using
lru_cachefor a method cache; it can instead just mention the possibility, and link to the FAQ entry for more detailed discussion.