caching.py 文件源码

python
阅读 29 收藏 0 点赞 0 评论 0

项目:phredutils 作者: doctaphred 项目源码 文件源码
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)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号