cached-property
cached-property copied to clipboard
Add a way to check if a property has already been cached
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?
Works for me. I like how it plugs right into the very obvious way of accessing things in the object
s __dict__
attribute.
👍
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()?