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
评论列表
文章目录