Рассмотрим следующую систему кэширования и удаления свойств из класса:
class cached_property(object):
"""
Descriptor (non-data) for building an attribute on-demand on first use.
"""
def __init__(self, factory):
"""
<factory> is called such: factory(instance) to build the attribute.
"""
self._attr_name = factory.__name__
self._factory = factory
def __get__(self, instance, owner):
# Build the attribute.
attr = self._factory(instance)
# Cache the value; hide ourselves.
setattr(instance, self._attr_name, attr)
return attr
class test():
def __init__(self):
self.kikou = 10
@cached_property
def caching(self):
print("computed once!")
return True
def clear_cache(self):
try: del self.caching
except: pass
b = test()
print(b.caching)
b.clear_cache()
Это правильный способ удалить это кэшированное свойство? Я не уверен в том, как я это сделал ..