我可以从python上下文管理器中检索__exit__的返回值吗?

时间:2017-09-06 07:53:51

标签: python with-statement contextmanager

我在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

那么我怎样才能访问my_results,这些my_results是在我的with block之后(或结尾)从__exit__返回的。在__exit__方法中,这甚至是否合法返回其他真实的东西?

1 个答案:

答案 0 :(得分:5)

  

__exit__方法中返回其他真实的东西是否合法?

不,不是真的,但Python只会测试truth value,所以你可以逃脱它。换句话说,如果你在这里返回一个真实的对象,任何例外将被抑制。如果没有例外,那么返回一个真正的值只是一个无操作。

  

我怎样才能访问在我的with block之后(或结尾)从__exit__返回的my_results。

你做不到。 with表达式机器消耗它。

你应该以其他方式提供它;将其设置为上下文管理器对象本身的属性:

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
        self.my_results = my_results
        # returning None, we don't want to suppress exceptions
        return None

with MyContextManager(value1=my_value1, value2=my_value2) as manager:
     # Do some stuff

results = manager.my_results

manager块完成后,with名称可用。

这就是unittest.TestCase.assertRaises() context manager共享捕获的异常的方式,例如。

相关问题