在__enter__中输入上下文管理器

时间:2017-12-07 23:35:51

标签: python macros contextmanager

将上下文管理器定义为函数,很容易以编程方式从一个内部输入单独的(或递归的)上下文管理器,如下所示:

@contextmanager
def enter(times):
    if times:
        with enter(times - 1) as tup:
            print 'entering {}'.format(times)
            yield tup + (times,)
            print 'exiting {}'.format(times)
    else:
        yield ()

运行此:

In [11]: with enter(4) as x:
....:     print x
....:
entering 1
entering 2
entering 3
(1, 2, 3)
exiting 3
exiting 2
exiting 1

所有进入/退出记账都是为您完成的,多好!但是如果你有一个课程而不是一个职能呢?

class Enter(object):
    def __init__(self, times):
        self.times = times

    def __enter__(self):
        print 'entering {}'.format(self.times)
        if self.times:
            with Enter(self.times - 1) as tup:  # WRONG
                return tup + (self.times,)
        return ()

    def __exit__(self, *_):
        print 'exiting {}'.format(self.times)

运行这个是错误的,因为在运行with-block中的任何代码之前输入并退出嵌套调用:

In [12]: with Enter(3) as tup:
    print tup
....:
entering 3
entering 2
entering 1
entering 0
exiting 0
exiting 1
exiting 2
(1, 2, 3)
exiting 3

规定:强迫客户自己使用ExitStack是不可接受的;内部调用必须像生成器情况一样进行封装。涉及Enter维护自己的私有堆栈的解决方案也不是最理想的(在现实生活中,内部__exit__调用必须与线程安全中的内部__enter__调用匹配方式,但即使在这个简单的例子中,我也希望尽可能避免这种手工簿记。)

2 个答案:

答案 0 :(得分:1)

__enter__中使用嵌套的上下文管理器似乎很神奇。

检查一下:

class Enter(object):
    def __init__(self, times):
        self.times = times

    def __enter__(self):
        print('entering {}'.format(self.times))
        if self.times:
            with Enter(self.times - 1) as tup:  # WRONG
                print('returning {}'.format(tup))
                return tup + (self.times,)
        print('returning () from times={}'.format(self.times))
        return ()

    def __exit__(self, *_):
        print('exiting {}'.format(self.times))

with Enter(3) as tup:
    print(tup)

运行此打印

entering 3
entering 2
entering 1
entering 0
returning () from times=0
returning ()
exiting 0
returning (1,)
exiting 1
returning (1, 2)
exiting 2
(1, 2, 3)
exiting 3

我认为这在某种程度上是有意义的。当您调用with Enter(3) ...时,思维模型可能必须“完成” __enter__方法,而“完成”意味着进入和退出所有上下文管理器。

def foo():
    with Enter(2) as tup:
        return tup
# we expect Enter to exit before we return, so why would it be different when
# we rename foo to __enter__?

让我们明确地做到这一点。

In [3]: %paste
class Enter(object):

    def __init__(self, times):
        self.times = times
        self._ctx = None

    def __enter__(self):
        print('entering {}'.format(self.times))
        if self.times:
            self._ctx = Enter(self.times - 1)
            tup = self._ctx.__enter__()
            return tup + (self.times,)
        else:
            return ()

    def __exit__(self, *_):
        if self._ctx is not None:
            self._ctx.__exit__()
        print('exiting {}'.format(self.times))

In [4]: with Enter(3) as tup:
   ...:     print(tup)
   ...:
entering 3
entering 2
entering 1
entering 0
(1, 2, 3)
exiting 0
exiting 1
exiting 2
exiting 3

(在@jasonharper的指导下得到解答。)

答案 1 :(得分:0)

我很惊讶尚未在标准库中添加此功能,但是当我需要一个类作为上下文管理器时,我正在使用以下实用程序:

class ContextManager(metaclass=abc.ABCMeta):
  """Class which can be used as `contextmanager`."""

  def __init__(self):
    self.__cm = None

  @abc.abstractmethod
  @contextlib.contextmanager
  def contextmanager(self):
    raise NotImplementedError('Abstract method')

  def __enter__(self):
    self.__cm = self.contextmanager()
    return self.__cm.__enter__()

  def __exit__(self, exc_type, exc_value, traceback):
    return self.__cm.__exit__(exc_type, exc_value, traceback)

用法:

class MyClass(ContextManager):

  @contextlib.contextmanager
  def contextmanager(self):
    try:
      print('Entering...')
      yield self
    finally:
      print('Exiting...')


with MyClass() as x:
  print(x)