-
-
Notifications
You must be signed in to change notification settings - Fork 30.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bpo-44310: Add a FAQ entry for caching method calls #26731
Conversation
Update to 15 March
Thanks @rhettinger for the PR 🌮🎉.. I'm working now to backport this PR to: 3.10, 3.9. |
(cherry picked from commit 7f01f77) Co-authored-by: Raymond Hettinger <rhettinger@users.noreply.github.com>
GH-26777 is a backport of this pull request to the 3.10 branch. |
(cherry picked from commit 7f01f77) Co-authored-by: Raymond Hettinger <rhettinger@users.noreply.github.com>
GH-26778 is a backport of this pull request to the 3.9 branch. |
pretty late to this but a hashable-mutable is usually a nono and the example is broken because of it: from functools import lru_cache
class Weather:
"Example with a mutable station identifier"
def __init__(self, station_id):
self.station_id = station_id
def change_station(self, station_id):
self.station_id = station_id
def __eq__(self, other):
return self.station_id == other.station_id
def __hash__(self):
return hash(self.station_id)
@lru_cache(maxsize=20)
def historic_rainfall(self, date, units='cm'):
print('...recomputing')
return f'Rainfall on a given date (for: {self.station_id}, {date}, {units})'
print("Weather(1)")
w1 = Weather(1)
print(w1.historic_rainfall('day1'))
w1.change_station(2)
print("Weather(1) again")
print(Weather(1).historic_rainfall('day1'))
output = '''\
$ python3 t3.py
Weather(1)
...recomputing
Rainfall on a given date (for: 1, day1, cm)
Weather(1) again
...recomputing
Rainfall on a given date (for: 1, day1, cm)
''' not to mention the lifetime of the original |
https://bugs.python.org/issue44310