~/Blog

Brandon Rozek

Photo of Brandon Rozek

PhD Student @ RPI studying Automated Reasoning in AI and Linux Enthusiast.

Quick Python: Cached Property

Published on

Updated on

If you have a property in an object that only needs to be computed once, consider using cached_property to store the result and serve for future function calls.

import functools
class Number:
    def __init__(self, n):
        self.n = n
    @functools.cached_property
    def is_prime(self):
    	return all(self.n % i for i in range(2, self.n))

Let’s test it with the Mersenne prime 524287.

n = Number(524287)
n.is_prime

After maybe 1-2 seconds of thinking you should get True.

Run it again and the result will be instantaneous!

Reply via Email Buy me a Coffee
Was this useful? Feel free to share: Hacker News Reddit Twitter