Python 在uuu enter中输入上下文管理器__

Python 在uuu enter中输入上下文管理器__,python,macros,contextmanager,Python,Macros,Contextmanager,通过将上下文管理器定义为函数,可以很容易地以编程方式从一个上下文管理器中输入一个单独的(或递归的)上下文管理器,如下所示: @contextmanager def enter(times): if times: with enter(times - 1) as tup: print 'entering {}'.format(times) yield tup + (times,) print 'exit

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

@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块中的任何代码之前,先输入和退出嵌套调用:

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
的解决方案也是次优的(在现实生活中,必须以线程安全的方式将内部
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu

看看这个:

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
我认为这在某种程度上是有意义的。心智模型可能是,当你用Enter(3)
调用
时,必须“完成”
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu

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的指导下回答。)

我很惊讶标准库中还没有添加这一功能,但当我需要一个类作为上下文管理器时,我使用以下util:

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)

你不能在你的
\uuuu enter\uuuuuuuuuuuuuuuuuuu()
方法中使用
with
,因为它太多了。你需要:1)创建另一个
enter()
实例,2)手动调用它的
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu()
,3)将它保存在一个实例变量中,这样。