在我正在构建的图形API中,我想制作一个也可以用作contextmanager的方法:
该方法当前如下所示:
class Gfx:
def __init__(self):
self._fill = None
def fill(self, color):
self._fill = color
我可以创建一个contextmanager,将在使用后恢复状态,如下所示:
class Gfx:
def __init__(self):
self._fill = None
@contextmanager
def fill(self, color):
"""Allow user to set fill color, then restore it"""
old_fill = self._fill
self._fill = color
yield
self._fill = old_fill
但是,如何根据它的调用方式使它同时工作呢?
>>> gfx = Gfx()
>>> gfx.fill("red") # use as ordinary method
>>> print(self._fill)
"red"
>>> with gfx.fill("blue") # use as context manager
... print(gfx._fill)
"blue"
>>> print(gfx._fill)
"red"
您需要混合使用各种方法。有一个称为的函数,返回一个上下文管理器,当不使用时,将忽略上下文管理器with
。类似于以下内容:
class Gfx:
def __init__(self):
self._fill = None
@contextmanager
def _fillctx(self, old_fill):
try:
yield
finally:
self._fill = old_fill
def fill(self, color):
"""Allow user to set fill color, then restore it"""
old_fill = self._fill
self._fill = color
return self._fillctx(old_fill)
如果不使用调用with
,self._fill
则会设置并返回一个从未使用过的上下文管理器。当使用调用时with
,它将在调用该上下文管理器的self._fill
时重置__exit__
。
需要明确的是,最好只使用单独的方法,这样您的“普通方法”方法会更有效,并且可以使上下文管理器方法更安全(实际上__enter__
按照您的预期进行设置工作,缩小了阻止__exit__
被调用的竞争条件的窗口)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句