我想要:
# Simple example, one could replace try/except by any other nested construct
def mycontextmanager_generator(foo):
try:
yield
except:
print 'bar'
raise
mycontextmanager = build_contextmanager(mycontextmanager_generator)
mydecorator = build_decorator(mycontextmanager_generator)
>>> with mycontextmanager():
>>> raise Exception('baz gone bar in context manager')
... bar
>>> @mydecorator()
>>> def bazzer():
>>> raise Exception('baz gone bar in decorator')
>>> bazzer()
... bar
在这个例子中,我从一个生成器函数构建了一个上下文管理器,从同一个函数构建了一个decorator。这是我试图以不成功的方式实现的。在
更一般地说,我想要的是干的:写一次try/except
块,然后通过decorator和上下文管理器重用它一次:无论是在生成器函数中还是在任何其他包装器中,只编写一次try/except bloc。在
ContextDecorator
东西(py3/contextlib2
中的contextlib
中)只能用于类,但在这种情况下它似乎没有用。。。我错过什么了吗?有没有一种方法可以使用__enter__
和__exit__
使用基于类的ContextManager实现try/except块吗?在
或者,是否有可能将使用yield
语法构建的contextmanager转换为decorator?在
或者相反(decorator到contextmanager)?在
如果没有,很乐意知道Python在这方面的局限性是什么。在
据我所知,yield
语法与Python解释器和上下文切换非常紧密地绑定在一起,我不知道是否可以在这一点上改变它的行为。在
通过组合
contextmanager
用于管理其上下文的类(_GeneratorContextManager
)和ContextDecorator
类,可以轻松地实现所需的功能。例如产生:
^{pr2}$当装饰用的时候
产生:
一个比Dunes的更容易理解的解决方案,尽管没有利用
ContextDecorator
双语法。在给出:
^{pr2}$相关问题 更多 >
编程相关推荐