是否可以制作一个上下文敏感的 python 上下文管理器来保存、修改和恢复状态?
我有一对 python 函数,它们当前在两个值之间翻转全局变量。我想将它们变成上下文管理器,这样我就可以将它们用作 with
块,在块内设置变量,但之后恢复它。这是期望的行为:
>>> MODE
'user'
>>> mode_sudo() # Sets MODE to 'sudo'...
>>> MODE
'sudo'
>>> mode_user() # Sets MODE to 'user'...
>>> MODE
'user'
>>> with mode_sudo():
... print MODE
'sudo'
>>> MODE
'user'
这样的嵌合体可能吗?
更新:为了清楚起见,这里是仅上下文管理器的实现:
from contextlib import contextmanager
@contextmanager
def mode_sudo():
global MODE
old_mode = MODE
MODE = 'sudo'
yield
MODE = old_mode
@contextmanager
def mode_user():
global MODE
old_mode = MODE
MODE = 'user'
yield
MODE = old_mode
使用关键字调用这些 w/oa 会返回一个生成器。有没有办法通过普通函数调用和巧克力上下文管理器来获得模式翻转行为?
I have a pair of python functions that currently flip a global variable between two values. I would like to turn them into context managers so I can use them as with
blocks, setting the variable inside the block, but restoring it after. Here's the desired behaviour:
>>> MODE
'user'
>>> mode_sudo() # Sets MODE to 'sudo'...
>>> MODE
'sudo'
>>> mode_user() # Sets MODE to 'user'...
>>> MODE
'user'
>>> with mode_sudo():
... print MODE
'sudo'
>>> MODE
'user'
Is such a chimera possible?
UPDATE: Just for clarity, here's the context-manager-only implementation:
from contextlib import contextmanager
@contextmanager
def mode_sudo():
global MODE
old_mode = MODE
MODE = 'sudo'
yield
MODE = old_mode
@contextmanager
def mode_user():
global MODE
old_mode = MODE
MODE = 'user'
yield
MODE = old_mode
Calling these w/o a with keyword returns a generator. Is there a way to get the mode-flipping behavior with both the plain-vanilla function call and the chocolate context manager?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这样做:
Do it like this:
简单的方法:
同上
mode_sudo()
。有关更多详细信息,请参阅文档。它实际上是整个“定义一个实现 __enter__ 和 __exit__ 的类”的快捷方式。Easy way:
idem for
mode_sudo()
. See the doc for more details. It is actually a shortcut for the whole "definine a class that implements__enter__
and__exit__
"-thing.