gpt4 book ai didi

python - 如果生成器被垃圾收集,上下文管理器是否退出?

转载 作者:行者123 更新时间:2023-12-02 01:59:32 26 4
gpt4 key购买 nike

假设我们有一个像这样的简单生成器:

def generator():
with open('example.txt', 'r') as f:
yield f.readline()
yield f.readline()

并像这样使用它来获取单个元素/行:

line = next(generator())

一旦临时生成器对象被垃圾回收,上下文管理器是否关闭?

最佳答案

with 语句与 try 语句具有相同的语义,除了 with 语句本身的主体之外还有一些额外的代码执行.来自 https://docs.python.org/3/reference/compound_stmts.html#the-with-statement"

The following code:

with EXPRESSION as TARGET:
SUITE

is semantically equivalent to:

manager = (EXPRESSION)
enter = type(manager).__enter__
exit = type(manager).__exit__
value = enter(manager)
hit_except = False

try:
TARGET = value
SUITE
except:
hit_except = True
if not exit(manager, *sys.exc_info()):
raise
finally:
if not hit_except:
exit(manager, None, None, None)

因此我们的 yield 表达式位于 with 语句隐含的 try 语句中。

来自 https://docs.python.org/3/reference/expressions.html#yieldexpr

Yield expressions are allowed anywhere in a try construct. If the generator is not resumed before it is finalized (by reaching a zero reference count or by being garbage collected), the generator-iterator’s close() method will be called, allowing any pending finally clauses to execute.

with 语句隐含的 finally 子句显式调用上下文管理器的 __exit__ 方法,该方法关闭文件。

关于python - 如果生成器被垃圾收集,上下文管理器是否退出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69154996/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com