Created
July 8, 2020 08:03
-
-
Save Chestermozhao/f63462ee40e6491025d5c19ca881fef0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from cached_property import cached_property | |
class Monopoly(object): | |
def __init__(self): | |
self.boardwalk_price = 500 | |
@cached_property | |
def boardwalk(self): | |
# Again, this is a silly example. Don't worry about it, this is | |
# just an example for clarity. | |
print("======hello world======") | |
self.boardwalk_price += 50 | |
return self.boardwalk_price | |
monopoly = Monopoly() | |
monopoly.boardwalk | |
monopoly.boardwalk | |
# cached_property will save the calculated result in __dict__ | |
# you can delete the value in __dict__, and call method again to refesh it | |
del monopoly.__dict__["boardwalk"] | |
monopoly.boardwalk |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment