def cache(func=..., *, key='received'):
"""Memoize the function using weak references.
Once the decorated function has been called with a given signature,
it will return the same object for all subsequent calls with that
signature, as long as another non-weak reference to the object still
exists.
>>> class WeakReferenceableList(list):
... pass # Built-in lists can't be weak-referenced
>>> @cache
... def say(word, also='lmao'):
... return WeakReferenceableList([word, also])
>>> say('ayy') is say('ayy')
True
>>> say('ayy') is say('ayy', 'lmao')
True
>>> say('ayy') is say('ayy', also='lmao')
True
>>> say('ayy') is say(also='lmao', word='ayy')
True
"""
return memoize(func, cache=weakref.WeakValueDictionary(), key=key)
评论列表
文章目录