1
我在python中使用上下文管理器。在想从我的__exit__
方法中获取一些日志。所以,我的代码记录是这样的:我可以从python上下文管理器中获取__exit__的返回值吗?
class MyContextManager:
def __init__(self, value1, value2)
self.value1 = value1
self.value2 = value2
def __enter__(self)
# Do some other stuff
return self
def __exit__(self, exc_type, exc_val, exc_tb):
# Do some tear down action, process some data that is
# created in __enter__ and log those results
return my_results
with MyContextManager(value1=my_value1, value2=my_value2) as manager:
# Do some stuff
因此,如何我可以访问它从__exit__
与块返回后(或结尾)我的my_results。这是否合法返回__exit__
方法中的其他值True?
非常感谢!有用的信息! :)我不知道MyContextManager的实例在with块后仍然存在。 –
很高兴有帮助!如果您觉得它对您有用,请随时[接受我的回答](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work)。 :-) –