Python 3.4 提供了这个简洁的工具来临时重定向 stdout:
# From https://docs.python.org/3.4/library/contextlib.html#contextlib.redirect_stdout
with redirect_stdout(sys.stderr):
help(pow)
The code http://hg.python.org/cpython/file/3.4/Lib/contextlib.py并不是超级复杂,但我不想一遍又一遍地写它,特别是因为已经对其进行了一些思考以使其可重入:
class redirect_stdout:
def __init__(self, new_target):
self._new_target = new_target
# We use a list of old targets to make this CM re-entrant
self._old_targets = []
def __enter__(self):
self._old_targets.append(sys.stdout)
sys.stdout = self._new_target
return self._new_target
def __exit__(self, exctype, excinst, exctb):
sys.stdout = self._old_targets.pop()
我想知道是否有通用的方法来使用with
临时改变变量值的语句。另外两个用例来自sys
are sys.stderr
and sys.excepthook
.
在完美的世界中,这样的事情会起作用:
foo = 10
with 20 as foo:
print(foo) # 20
print (foo) # 10
我怀疑我们能否做到这一点,但也许这样的事情是可能的:
foo = 10
with temporary_set('foo', 20):
print(foo) # 20
print (foo) # 10
我可以通过扎根来解决这个问题globals()
,但这不是任何人都会选择使用的。
更新:虽然我认为我的“foo = 10”示例阐明了我想要做的事情,但它们没有传达实际的用例。这里有两个:
- 重定向 stderr,很像redirect_stdout
- 暂时更改sys.excepthook。我以交互方式进行了大量开发,当我向 excepthook 添加某些内容(通过将原始函数包装在我自己的函数中,例如使用日志记录模块记录异常)时,我通常希望它在某个时刻被删除。这样我就不会有越来越多的函数副本自我包装。这个问题 https://stackoverflow.com/questions/8415463/adding-function-to-sys-excepthook/面临一个密切相关的问题。