GH avatar Bite 207. Cached property decorator

Given the sample class Planet, computing the mass attribute of an instance is a computationally expensive and time consuming operation. Ideally you'd like this attribute to be a cached_property that only gets computed once and is then stored for future access, not having to be recalculated over (and over and over).


Tasks

Complete cached_property(func) as a decorator function, so that asking "What was the mass of Planet('red') again?" is consistent and quick.

Note: Attempting to set mass should raise AttributeError as it is read-only.

Hint: replace the @property decorator with your implementation of @cached_property and remove the mass setter entirely, attempting to set mass should raise AttributeError as it is read-only.

9 out of 9 users completed this Bite.
Will you be Pythonista #10 to crack this Bite?
It takes an average of ~65 minutes to solve this Bite (submissions 5-240 min).
Pythonistas rate this Bite 5.0 on a 1-10 difficulty scale.
» Up for a challenge? 💪

Buy Now Login and get coding
We use Python 3.7