- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
使用 with 语句,我们可以仅使用一层缩进/嵌套来输入许多上下文处理程序:
>>> from contextlib import contextmanager
>>> @contextmanager
... def frobnicate(n):
... print('frobbing {}'.format(n))
... yield
...
>>> frob1 = frobnicate(1)
>>> frob2 = frobnicate(2)
>>> with frob1, frob2:
... pass
...
frobbing 1
frobbing 2
但这似乎不起作用:
>>> frobs = [frobnicate(1), frobnicate(2)]
>>> with *frobs:
... pass
# SyntaxError: invalid syntax
我们如何才能输入 n 个上下文管理器而不必手动写出每个上下文管理器?
最佳答案
python2.7 有 contextlib.nested然而,要做到这一点,由于容易出错的怪癖,它已被弃用。
This function has two major quirks that have led to it being deprecated. Firstly, as the context managers are all constructed before the function is invoked, the
__new__()
and__init__()
methods of the inner context managers are not actually covered by the scope of the outer context managers. That means, for example, that usingnested()
to open two files is a programming error as the first file will not be closed promptly if an exception is thrown when opening the second file.Secondly, if the
__enter__()
method of one of the inner context managers raises an exception that is caught and suppressed by the__exit__()
method of one of the outer context managers, this construct will raise RuntimeError rather than skipping the body of the with statement.
python3.3 在 contextlib.ExitStack 上做得更好看起来像:
from contextlib import ExitStack
with ExitStack() as stack:
contexts = [stack.enter_context(frobnicate(i)) for i in range(2)]
...
参见 contextlib2用于向后移植到 python2.x 代码。
关于python - 如何 __enter__ n 上下文管理器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26855747/
在contextlib.py ,我看到 ExitStack 类正在通过类型对象 (type(cm)) 调用 __enter__() 方法,而不是直接调用给定对象 (cm) )。 我想知道为什么或为什么
def save_list(): f = open('data.txt', 'w') ii = 0 with itemMatrix[ii] as item: f.write(it
刚刚学习 with 语句 especially from this article 问题是,我可以将参数传递给 __enter__ 吗? 我有这样的代码: class clippy_runner:
我没有重新分配 open 关键字,但仍然收到此错误。有什么建议或指导可以解决我的错误吗? with tempfile.mkdtemp() as test_dir: print(tes
将上下文管理器定义为函数,很容易以编程方式从一个上下文管理器中输入一个单独的(或递归的)上下文管理器,如下所示: @contextmanager def enter(times): if ti
这个问题在这里已经有了答案: Object becomes None when using a context manager (3 个答案) 关闭 4 年前。 我在尝试运行代码时收到属性错误。
是否可以保证 __exit__() 方法在 __enter__() 有异常的情况下被调用? >>> class TstContx(object): ... def __enter__(self)
我最近想知道当 __enter__ 引发异常时不隐式调用 __exit__ 的原因是什么?为什么要这样设计?我正在实现服务运行器类以供 'with' 关键字使用,结果 __exit__ 从未被调用。
我是单元测试的新手,我正在尝试找到一种方法来测试 with 关键字是否在我的对象中正常工作。 在这种情况下,我的对象有一个创建临时目录的 __enter__ 方法和应该销毁它的 __exit__ 方法
我运行这个: import numpy as np import sys temp = np.array([[10, 20], [30, 40]]) with np.set_printoptions(
使用 with 语句,我们可以仅使用一层缩进/嵌套来输入许多上下文处理程序: >>> from contextlib import contextmanager >>> @contextmanager
我想在使用 __getattr__ 重定向调用的对象上使用 with。 但是,这似乎不适用于 __enter__ 方法 请考虑以下简化代码来重现错误: class EnterTest(object):
我用谷歌搜索了 calling __enter__ manually但没有运气。所以让我们假设我有 MySQL 连接器类,它使用 __enter__ 和 __exit__ 函数(最初与 with 语句
据我了解,上下文管理器的 __init__() 和 __enter__() 方法只被调用一次,一个接一个,没有任何机会其他要在两者之间执行的代码。将它们分成两种方法的目的是什么,我应该在每种方法中放入
关注 this related question ,虽然总是有一些库以独特的方式使用语言特性的例子,但我想知道是否返回 self 以外的值在 __enter__方法应该被认为是一种反模式。 这在我看来
有没有一种方法可以在上下文管理器的 __enter__ 方法中捕获异常,而无需将整个 with block 包装在 try 中? class TstContx(object): def __e
我问了this昨天的问题。但现在我意识到这是一个错误的问题。 __enter__ 函数通常包含 try block 吗? 最佳答案 如果您准备好从异常中正常恢复,他们可以,但是通常您会希望允许异常向上
问题几乎说明了一切。 我想以这种方式查看代码: >>>f = open("x.txt") >>>print contents of f.__enter__() #>> f = open("x.txt"
我有一个 python 包,我想在 Matlab 中使用它的类和方法。我知道这可以从 Matlab 2014b 开始直接完成。我的意思是您所要做的就是在语句的开头添加 py. 。然而,到目前为止一切顺
假设: class A(object): def __init__(self): self.cnt = 0 def __enter__(self): self.cnt += 1
我是一名优秀的程序员,十分优秀!