让对象支持上下文管理

让我们的对象支持上下文管理(with语句)

为了让一个对象兼容 with 语句,你需要实现 __enter__() 和 __exit__() 方法

demo:

class WithTest:
    def __enter__(self):
        print("enter")
        return self  #这里需要return
    def __exit__(self, exc_type, exc_val, exc_tb):
        print("exit")
    def work(self):
        print("working")


with WithTest() as withtest: #withtest 就是 __enter__返回的东西
    withtest.work()

上面的输出内容:

enter
working
exit

 

编写上下文管理器的主要原理是你的代码会放到 with 语句块中执行。 当出现 with 语句的时候,对象的 __enter__() 方法被触发,

它返回的值(如果有的话)会被赋值给 as 声明的变量。然后,with 语句块里面的代码开始执行。 最后,__exit__() 方法被触发进行清理工作。

不管 with 代码块中发生什么,上面的控制流都会执行完,就算代码块中发生了异常也是一样的。 事实上,__exit__() 方法的三个参数包含了异常类型、

异常值和追溯信息(如果有的话)。 __exit__() 方法能自己决定怎样利用这个异常信息,或者忽略它并返回一个None值。

如果 __exit__() 返回 True ,那么异常会被清空,就好像什么都没发生一样, with 语句后面的程序继续在正常执行。

 

另一个demo

from socket import socket, AF_INET, SOCK_STREAM

class LazyConnection:
    def __init__(self, address, family=AF_INET, type=SOCK_STREAM):
        self.address = address
        self.family = family
        self.type = type
        self.sock = None

    def __enter__(self):
        if self.sock is not None:
            raise RuntimeError('Already connected')
        self.sock = socket(self.family, self.type)
        self.sock.connect(self.address)
        return self.sock

    def __exit__(self, exc_ty, exc_val, tb):
        self.sock.close()
        self.sock = None

 

posted on 2021-01-02 21:16  思此狂  阅读(76)  评论(0编辑  收藏  举报

导航