上下文管理协议

一、上下文管理协议其实就是  with  obje as  f

在文件操作时用过,with open("a.txt","r") as f:

当时的好处就是不用手动关闭文件了

学到现在我们知道了,其实open也是一个类,之所以能用 with as 是类里做了“小手脚”

class Foo:
    def __init__(self,name):
        self.name = name

    def __enter__(self):
        print("执行enter")
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        print("执行exit")

with Foo('a.txt') as f:
    print(f)
    print(f.name)
    print("======>")
    print("======>")

print("000000000000000")

上述过程:

1.  with obj ------->触发 obj.__enter__(),  拿到的返回值赋值给 f  (with obj as f  等同于 f= obj.__enter__())

2. with obj as f 内的代码执行完毕后触发  __exit__()

二、__exit__中的异常处理

先了解下异常

分为:异常类,异常值,追踪信息

 

①没有异常的情况下,整个代码块运行完毕后去触发 __exit__,它的三个参数都为None

class Foo:
    def __init__(self,name):
        self.name = name

    def __enter__(self):
        print("执行enter")
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        print("执行exit")
        print(exc_type) #异常类
        print(exc_val)  #异常值
        print(exc_tb)  #追踪信息

with Foo('a.txt') as f:
    print(f)
    print(f.name)
    # print(jinling)  #触发 __exit__()
    print("======>")
    print("======>")

print("000000000000000")

  

②在有异常的情况下,从异常出现的位置直接触发 __exit__

a:如果__exit__的返回值为True 代表吞掉了异常

b:如果__exit__的返回值不为True,代表突出了异常

c: __exit__运行完毕代表了整个 with 语句的执行完毕

class Foo:
    def __init__(self,name):
        self.name = name

    def __enter__(self):
        print("执行enter")
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        print("执行exit")
        print(exc_type) #异常类
        print(exc_val)  #异常值
        print(exc_tb)  #追踪信息
        # return True

with Foo('a.txt') as f:
    print(f)
    print(f.name)
    print(jinling)  #触发 __exit__()
    print("======>")
    print("======>")

print("000000000000000")

  

class Foo:
    def __init__(self,name):
        self.name = name

    def __enter__(self):
        print("执行enter")
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        print("执行exit")
        print(exc_type) #异常类
        print(exc_val)  #异常值
        print(exc_tb)  #追踪信息
        return True

with Foo('a.txt') as f:
    print(f)
    print(f.name)
    print(jinling)  #触发 __exit__()
    print("======>")
    print("======>")

print("000000000000000")   #因为__exit__ 吞掉了异常,所以最后一句00000000000正常输出,但是with块内异常下面的代码不能正常输出

  

  

posted @ 2019-10-09 23:25  绝世刘  阅读(329)  评论(0编辑  收藏  举报