Skip to content
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

Merged
merged 25 commits into from
Jun 17, 2021

Conversation

rhettinger
Copy link
Contributor

@rhettinger rhettinger commented Jun 15, 2021

@rhettinger rhettinger added the needs backport to 3.9 only security fixes label Jun 17, 2021
@rhettinger rhettinger merged commit 7f01f77 into python:main Jun 17, 2021
@miss-islington
Copy link
Contributor

Thanks @rhettinger for the PR 🌮🎉.. I'm working now to backport this PR to: 3.10, 3.9.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jun 17, 2021
(cherry picked from commit 7f01f77)

Co-authored-by: Raymond Hettinger <rhettinger@users.noreply.github.com>
@bedevere-bot
Copy link

GH-26777 is a backport of this pull request to the 3.10 branch.

@bedevere-bot bedevere-bot removed the needs backport to 3.10 only security fixes label Jun 17, 2021
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jun 17, 2021
(cherry picked from commit 7f01f77)

Co-authored-by: Raymond Hettinger <rhettinger@users.noreply.github.com>
@bedevere-bot bedevere-bot removed the needs backport to 3.9 only security fixes label Jun 17, 2021
@bedevere-bot
Copy link

GH-26778 is a backport of this pull request to the 3.9 branch.

@asottile-sentry
Copy link

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 Weather is extended indefinitely because it is stuck in the lru_cache

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir skip news
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants