标签:none exit 运行 -- jin with __init__ 执行 return
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")
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")
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块内异常下面的代码不能正常输出
标签:none exit 运行 -- jin with __init__ 执行 return
原文地址:https://www.cnblogs.com/dabai123/p/11645082.html