python - 在 __enter__ 中输入上下文管理器

标签 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是 Not Acceptable ;内部调用必须像在生成器中一样进行封装。涉及 Enter 维护其自己的私有(private)堆栈的解决方案也是次优的(在现实生活中,内部 __exit__ 调用必须匹配到内部 __enter__ 以线程安全的方式调用,但即使在这个简单的示例中,我也想尽可能避免这种手动簿记。)

最佳答案

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

关于python - 在 __enter__ 中输入上下文管理器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47705573/

相关文章:

macros - 为什么 foo 与这个 Racket 宏中的 (foo) 具有相同的值?

python - TemporaryFileWrapper 实例没有 __call__ 方法

Python - select() 没有捕捉到损坏的套接字

vba - 将文件创建日期添加到列表中

Python - 将有符号整数转换为字节

scala - 用户包含 scalamacros paradise 的最简单方法

python - 捕获上下文管理器期间发生的异常 - Python

python - 使用上下文管理器管理多个资源的正确方法

python - IPython 和 matplotlib 配置文件和文件

python - 如何使用 "where in"子句