Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Chestermozhao/f63462ee40e6491025d5c19ca881fef0 to your computer and use it in GitHub Desktop.
Save Chestermozhao/f63462ee40e6491025d5c19ca881fef0 to your computer and use it in GitHub Desktop.
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