cached-property icon indicating copy to clipboard operation
cached-property copied to clipboard

Add a way to check if a property has already been cached

Open Tinche opened this issue 8 years ago • 2 comments

Sometimes it's useful to be able to check if a property has been cached already. Currently I'm using something like:

    @cached_property
    def cachable(self): pass

    def a_function(self):
        if 'cachable' in self.__dict__:
            # Already cached.
        else:
            # Not cached yet.

(Haven't tested this, but I think it'ill work) Can we think of a better API?

Tinche avatar May 13 '16 12:05 Tinche

Works for me. I like how it plugs right into the very obvious way of accessing things in the object s __dict__ attribute.

👍

pydanny avatar May 13 '16 23:05 pydanny

It works, I just consider it ugly and it exposes an implementation detail. I'm not really a fan of invalidation either (del self.__dict__['cachable']).

cached_property.iscached(self.cachable)? self.cachable.iscached has some issues. (What if the cachable has a different iscached attribute? We could do what namedtuple does and call it _iscached, not because it's protected but to avoid clashes.) Could we do the same for invalidate()?

Tinche avatar May 14 '16 17:05 Tinche