Title: we should not recommend using functools.cache on methods · Issue #104112 · python/cpython · GitHub
Open Graph Title: we should not recommend using functools.cache on methods · Issue #104112 · python/cpython
X Title: we should not recommend using functools.cache on methods · Issue #104112 · python/cpython
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 usi...
Open Graph 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. Bu...
X Description: Documentation Currently the documentation for functools.cached_property recommends stacking @property and @functools.cache decorators to "achieve an effect similar to" functools.cached_pr...
Opengraph URL: https://github.com/python/cpython/issues/104112
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"we should not recommend using functools.cache on methods","articleBody":"# Documentation\r\n\r\nCurrently [the documentation for functools.cached_property](https://docs.python.org/3/library/functools.html#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.\r\n\r\nThe [FAQ entry on caching methods](https://docs.python.org/3/faq/programming.html#faq-cache-method-calls) 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.\r\n\r\nSpecific proposals:\r\n\r\n1. The `cached_property` documentation should not recommend using `functools.cache` on a method.\r\n2. It also doesn't need to give a full example of using `lru_cache` for a method cache; it can instead just mention the possibility, and link to the FAQ entry for more detailed discussion.\r\n\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-104113\n* gh-104182\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/carljm","@type":"Person","name":"carljm"},"datePublished":"2023-05-03T00:18:15.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":2},"url":"https://github.com/104112/cpython/issues/104112"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:483c492a-cdb2-6412-10e5-f8b032a15c8b |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B872:2ACFDC:A15337:D857C3:69699303 |
| html-safe-nonce | fbef694ca55ad87e6e7f5570da6952d63bc3115b19cfffe5ceab358ab1edd41c |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCODcyOjJBQ0ZEQzpBMTUzMzc6RDg1N0MzOjY5Njk5MzAzIiwidmlzaXRvcl9pZCI6IjY0MzU2ODY5MDM1OTAxNDE0NyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 4fe2e3ad60191142385254a4169633e9e50fa016207fa2f42d67886dc9a970ea |
| hovercard-subject-tag | issue:1693245468 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/104112/issue_layout |
| twitter:image | https://opengraph.githubassets.com/846ea234eefb41b0a2ea8ab9852be6030b62aa5e1ef982f153cf93d4875a09a2/python/cpython/issues/104112 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/846ea234eefb41b0a2ea8ab9852be6030b62aa5e1ef982f153cf93d4875a09a2/python/cpython/issues/104112 |
| og:image:alt | Documentation Currently the documentation for functools.cached_property recommends stacking @property and @functools.cache decorators to "achieve an effect similar to" functools.cached_property. Bu... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | carljm |
| hostname | github.com |
| expected-hostname | github.com |
| None | 3542e147982176a7ebaa23dfb559c8af16f721c03ec560c68c56b64a0f35e751 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | af80af7cc9e3de9c336f18b208a600950a3c187c |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width