decorations.py 文件源码

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

项目:easypy 作者: weka-io 项目源码 文件源码
def singleton_contextmanager_method(func):
    cached_attr_name = '__singleton_contextmanager_method__' + func.__name__

    # Wrap with a context manager to get proper IPython documentation
    @contextmanager
    @wraps(func)
    def inner(self):
        with _singleton_contextmanager_method_attr_lock:
            try:
                cm = getattr(self, cached_attr_name)
            except AttributeError:
                cm = singleton_contextmanager(partial(func, self))
                setattr(self, cached_attr_name, cm)
        with cm as val:
            yield val

    return inner
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号