Özellikleri otomatik yok dönüş değerlerini önbelleğe. Alıcının (ve ayarlayıcıların), mülke her erişildiğinde çağrılması amaçlanmıştır.
Ancak Denis Otkidach sadece bu amaç için harika bir önbelleğe nitelik dekoratör (PSF license altında ActiveState üzerinde başlangıçta da the Python Cookbook, 2nd edition yayınlanan ve) yazmıştır:
: Burada
class cache(object):
'''Computes attribute value and caches it in the instance.
Python Cookbook (Denis Otkidach) https://stackoverflow.com/users/168352/denis-otkidach
This decorator allows you to create a property which can be computed once and
accessed many times. Sort of like memoization.
'''
def __init__(self, method, name=None):
# record the unbound-method and the name
self.method = method
self.name = name or method.__name__
self.__doc__ = method.__doc__
def __get__(self, inst, cls):
# self: <__main__.cache object at 0xb781340c>
# inst: <__main__.Foo object at 0xb781348c>
# cls: <class '__main__.Foo'>
if inst is None:
# instance attribute accessed on class, return self
# You get here if you write `Foo.bar`
return self
# compute, cache and return the instance's attribute value
result = self.method(inst)
# setattr redefines the instance's attribute so this doesn't get called again
setattr(inst, self.name, result)
return result
kullanımını gösteren bir örnektir
def demo_cache():
class Foo(object):
@cache
def bar(self):
print 'Calculating self.bar'
return 42
foo=Foo()
print(foo.bar)
# Calculating self.bar
# 42
print(foo.bar)
# 42
foo.bar=1
print(foo.bar)
# 1
print(Foo.bar)
# __get__ called with inst = None
# <__main__.cache object at 0xb7709b4c>
# Deleting `foo.bar` from `foo.__dict__` re-exposes the property defined in `Foo`.
# Thus, calling `foo.bar` again recalculates the value again.
del foo.bar
print(foo.bar)
# Calculating self.bar
# 42
demo_cache()
memoize dekoratör denemek
basit piton özelliği için muhtemelen overkill. – SingleNegationElimination
Ayrıca, bu arada, ['functools.lru_cache (maxsize = 128, typed = False)'] (https://docs.python.org/3/library/functools.html#functools.lru_cache) dekoratörünün de bulunduğunu unutmayın. Ayrıca "maxsize = None" parametresine de izin verir) –