我试图用Python的contextlib.ContextDecorator班级。在
有没有办法在上下文管理器中访问修饰函数的参数?在
下面是我正在做的一个例子:
from contextlib import ContextDecorator
class savePen(ContextDecorator):
def __enter__(self):
self.prevPen = self.dc.GetPen() # AttributeError
return self
def __exit__(self, *exc):
self.dc.SetPen(self.prevPen)
return False
鉴于上述情况,这:
^{pr2}$应等同于:
def func(dc, param1, param2):
prevPen = dc.GetPen()
# do stuff, possibly changing the pen style
dc.SetPen(prevPen)
我在文档中搜寻了contextlib,没有发现任何有用的东西。在
有人知道如何从ContextDecorator类中访问修饰函数的属性吗?在
正如@chepner在this response中所说,ContextDecorator是sugar for
def func(dc, param1, param2):
with savePen():
...
它不能访问函数的参数。在
但是,在本例中,with savePen()
中运行的任何内容都可以访问函数参数dc
、param1
和{
例如,这是有效的:
def func(dc, param1, param2):
with savePen():
print(param1)
我用我以前在Python2中做过的东西,建造了自己的装饰工。在
我没有使用上下文管理器,而是使用了
try...finally
结构。在以下是我的想法(我去掉了所有能使docstring正确的东西):
contextlib.contextmanager
在这里似乎更合适。请注意,与其他任何东西一样,您不能从函数外部访问函数体的局部变量(无论如何,除了内省技巧之外)。在注意,使用
^{pr2}$ContextDecorator
,上下文管理器是不带参数的实例化的,即语法糖(根据文档)是
所以没有办法告诉
savePen
要使用哪个对象(dc
)。在相关问题 更多 >
编程相关推荐