stacklens / dusai-blog

杜赛的博客的评论区。详情见 Issue。
2 stars 0 forks source link

article/149/ #96

Open utterances-bot opened 3 years ago

utterances-bot commented 3 years ago

Python魔法方法漫游指南:描述符 - 杜赛的博客

https://www.dusaiphoto.com/article/149/

yaozeliang commented 3 years ago

有个问题比较好奇,文中的缓存实现方法如下:

from time import sleep

class Foo:
    @Cache
    def bar(self):
        sleep(5)
        return 'Just sleep 5 sec...'

这个和Python的lru_cache使用场景分别是哪些呢?

from functools import lru_cache

@lru_cache(maxsize=None)
def fib(n):
    if n < 2:
        return n
    return fib(n-1) + fib(n-2)