- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
鉴于以下 ContextManager
类,这里是 __exit__
的顺序以下示例中的调用 - 声明的相反顺序。
有趣的是,__exit__
可以多次调用。这在 2.7 的所有 Python 版本中都是确定性的吗?如果没有,来自 哪个版本这个顺序是否可靠确定?
a = A()
b = B()
c = C()
with a, b, c, a, a:
print("Start")
from typing import ContextManager
class A(ContextManager):
def __exit__(self, __exc_type, __exc_value, __traceback):
print("A exit")
class B(ContextManager):
def __exit__(self, __exc_type, __exc_value, __traceback):
print("B exit")
class C(ContextManager):
def __exit__(self, __exc_type, __exc_value, __traceback):
print("C exit")
a = A()
b = B()
c = C()
with a, b, c, a, a:
print("Start")
Python 3.7+ 输出:
Start
A exit
A exit
C exit
B exit
A exit
最佳答案
同一个 with
上的多个上下文管理器行被处理,就好像它们是嵌套的一样。所以这:
with a, b, c:
stuff()
相当于:
with a:
with b:
with c:
stuff()
也就是说,
__enter__
方法从左到右调用,当块结束时,
__exit__
方法被称为从右到左。
关于python - with-block 中多个 __exit__ 调用的顺序是否总是确定的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68248141/
是否有一种 Pythonic 的方式来自动 __exit__ 一个类的所有成员? class C: def __init__(self): self.a = open('foo
我明白了 __enter__ 和__exit__ 用于实现上下文管理器。 如果在 with 语句中发生异常,异常的类型、值和回溯将传递给 __exit__ 方法。 __exit__ 可以处理异常: 返
我尝试从以下位置运行示例:https://docs.python.org/3/library/socketserver.html#socketserver-tcpserver-example 在我的笔
我无法在测试中将 __exit__ 方法附加到 mockito 模拟,以便模拟在 with 语句中使用它时不会介意。 这是一个不断引发 AttributeError: __exit__: 的测试示例
我在提醒自己“关键字”时偶然发现了这篇文章 http://effbot.org/zone/python-with-statement.htm 。我只是有一个小问题,可能是显而易见的问题。文章指出,这“
我已经通过.csv文件来发布请求, input_file = data.get('file', None) with input_file as datasheet: header =
调查 documentation python 中的 with 语句: The context manager’s __exit__() method is invoked. 我想知道如果我这样做会发
考虑到这个小的 Python 类,每当我使用 Ctrl+C 停止脚本时,__exit__ 函数都会在引发异常之前运行: import time class MyClass(object): d
一些背景:我在一家大银行工作,我试图重新使用一些 Python 模块,我无法更改,只能导入。我也没有安装任何新实用程序/功能等的选项(在 Linux 上运行 Python 2.6)。 我目前有这个:
我正在制作类似任务调度程序的东西,使用生成器作为协程。在下面的代码中,我需要确定性地执行 print cleanup。 从我的交互看来,将对象释放给垃圾收集器会导致上下文管理器退出。但是,我知道最好不
我知道从上下文管理器的 __exit__() 方法中重新引发异常是不好的风格。因此,我想在实例上附加一个属性,该属性可以携带上下文信息,如果我让异常通过或捕获它,则该信息不可用。这将避免重新提高它。
考虑以下 Python 2.x 代码片段。 from __future__ import print_function class myfile(file): def __exit__(sel
使用 pool.map(funct, iterable) 时出现此错误: AttributeError: __exit__ 没有解释,只是堆栈跟踪到模块内的 pool.py 文件。 这样使用: wit
我最近想知道当 __enter__ 引发异常时不隐式调用 __exit__ 的原因是什么?为什么要这样设计?我正在实现服务运行器类以供 'with' 关键字使用,结果 __exit__ 从未被调用。
鉴于以下 ContextManager类,这里是 __exit__ 的顺序以下示例中的调用 - 声明的相反顺序。 有趣的是,__exit__可以多次调用。这在 2.7 的所有 Python 版本中都是
我是单元测试的新手,我正在尝试找到一种方法来测试 with 关键字是否在我的对象中正常工作。 在这种情况下,我的对象有一个创建临时目录的 __enter__ 方法和应该销毁它的 __exit__ 方法
类有一个可定义的函数__exit__,允许实现上下文管理器。 它接受必需的参数: def __exit__(self, exc_type, exc_val, exc_tb): 但我找不到关于这些参数是
如果它引发异常(可能多次,可能有延迟),我想在 exit() 方法中再次调用代码对象。我知道使用装饰器很容易做到,但我的动机是有时我只想重复一些我不想提取到单独函数并装饰它的代码片段。我正在寻找这些方
当使用不带 as 的 with 语句时,__enter__ 函数永远不会执行,但 __exit__方法会? 例子: with test: test.do_something test.__exit
我正在尝试使用 Mock 修补一些上下文管理器功能,以便我可以测试代码在给定好的、坏的和垃圾输入的情况下是否能做出明智的事情。这是带有 with 语句的测试代码。补丁已在我的代码中的正确位置完成。 @
我是一名优秀的程序员,十分优秀!