Nei*_*l G 5 python generator contextmanager python-3.x
假设我有一个上下文管理器:
@contextmanager
def cm(x):
y = f(x)
z = yield y
g(z)
Run Code Online (Sandbox Code Playgroud)
如何发送z到上下文管理器?
我试过:
my_cm = cm()
with my_cm:
my_cm.gen.send(123)
Run Code Online (Sandbox Code Playgroud)
但我得到 StopIteration,这可能是因为send产量?
@contextmanager返回一个辅助函数,该函数又返回一个GeneratorContextManager实例,该实例使用生成器作为管理上下文的方式。
它不打算用作您可以发送到的任何内容,next()在__enter__. 您可以尝试使用以下.gen属性访问底层生成器:
my_cm.gen.send(123)
Run Code Online (Sandbox Code Playgroud)
但您必须包含额外的yield 语句以防止该方法过早退出。请注意,将再次__exit__调用以结束生成器。next()
@contextmanager
def cm(x):
y = f(x)
a = yield y # Run for `__enter__`, returning `y`; `.send()` resumes here
yield # Here we pause again
g(a)
Run Code Online (Sandbox Code Playgroud)
有效,因为它有一秒钟的时间yield再次暂停生成器,直到上下文管理器准备好next()最后一次调用。
| 归档时间: |
|
| 查看次数: |
1476 次 |
| 最近记录: |