# cached-property
-[![](https://github.com/pydanny/cached-property/workflows/Python%20package/badge.svg)](https://github.com/pydanny/cached-property/actions)
+[![Github Actions status](https://github.com/pydanny/cached-property/workflows/Python%20package/badge.svg)](https://github.com/pydanny/cached-property/actions)
[![PyPI](https://img.shields.io/pypi/v/cached-property.svg)](https://pypi.python.org/pypi/cached-property)
-[![Code style: black ](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/ambv/black)
+[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/ambv/black)
A decorator for caching properties in classes.
price goes up by $50!
```python
- class Monopoly(object):
+class Monopoly(object):
- def __init__(self):
- self.boardwalk_price = 500
+ def __init__(self):
+ self.boardwalk_price = 500
- @property
- def boardwalk(self):
- # In reality, this might represent a database call or time
- # intensive task like calling a third-party API.
- self.boardwalk_price += 50
- return self.boardwalk_price
+ @property
+ def boardwalk(self):
+ # In reality, this might represent a database call or time
+ # intensive task like calling a third-party API.
+ self.boardwalk_price += 50
+ return self.boardwalk_price
```
Now run it:
Let's convert the boardwalk property into a `cached_property`.
```python
- from cached_property import cached_property
+from cached_property import cached_property
- class Monopoly(object):
+class Monopoly(object):
- def __init__(self):
- self.boardwalk_price = 500
+ 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.
- self.boardwalk_price += 50
- return self.boardwalk_price
+ @cached_property
+ def boardwalk(self):
+ # Again, this is a silly example. Don't worry about it, this is
+ # just an example for clarity.
+ self.boardwalk_price += 50
+ return self.boardwalk_price
```
Now when we run it the price stays at $550.
* Pip, Django, Werkzueg, Bottle, Pyramid, and Zope for having their own implementations. This package originally used an implementation that matched the Bottle version.
* Reinout Van Rees for pointing out the `cached_property` decorator to me.
-* My awesome wife [@audreyr](https://github.com/audreyfeldroy) who created [`cookiecutter`](https://github.com/cookiecutter/cookiecutter), which meant rolling this out took me just 15 minutes.
+* My awesome wife [@audreyfeldroy](https://github.com/audreyfeldroy) who created [`cookiecutter`](https://github.com/cookiecutter/cookiecutter), which meant rolling this out took me just 15 minutes.
* @tinche for pointing out the threading issue and providing a solution.
* @bcho for providing the time-to-expire feature
-## Support this project
-
-This project is maintained by volunteers. Support their efforts by spreading the word about:
-
-### Django Crash Course
-
-[![](https://cdn.shopify.com/s/files/1/0304/6901/products/AWoD-Front-5.5x8.5in_540x.jpg?v=1600471198)](https://www.feldroy.com/products/django-crash-course)
-
-Django Crash Course for Django 3.0 and Python 3.8 is the best cheese-themed Django reference in the universe!