目录
with open('a.txt') as f: '代码块'
class open: def __init__(self, name): self.name = name def __enter__(self): print('出现with语句,对象的__enter__被触发,有返回值则赋值给as声明的变量') # return self def __exit__(self, exc_type, exc_val, exc_tb): print('with中代码块执行完毕时执行我啊') with open('a.txt') as f: print('=====>执行代码块') # print(f,f.name)
出现with语句,对象的__enter__被触发,有返回值则赋值给as声明的变量 =====>执行代码块 with中代码块执行完毕时执行我啊
class open: def __init__(self, name): self.name = name def __enter__(self): print('出现with语句,对象的__enter__被触发,有返回值则赋值给as声明的变量') def __exit__(self, exc_type, exc_val, exc_tb): print('with中代码块执行完毕时执行我啊') print(exc_type) print(exc_val) print(exc_tb) try: with open('a.txt') as f: print('=====>执行代码块') raise attributeerror('***着火啦,救火啊***') except exception as e: print(e)
出现with语句,对象的__enter__被触发,有返回值则赋值给as声明的变量 =====>执行代码块 with中代码块执行完毕时执行我啊 <class 'attributeerror'> ***着火啦,救火啊*** <traceback object at 0x1065f1f88> ***着火啦,救火啊***
class open: def __init__(self, name): self.name = name def __enter__(self): print('出现with语句,对象的__enter__被触发,有返回值则赋值给as声明的变量') def __exit__(self, exc_type, exc_val, exc_tb): print('with中代码块执行完毕时执行我啊') print(exc_type) print(exc_val) print(exc_tb) return true with open('a.txt') as f: print('=====>执行代码块') raise attributeerror('***着火啦,救火啊***') print('0' * 100) #------------------------------->会执行
出现with语句,对象的__enter__被触发,有返回值则赋值给as声明的变量 =====>执行代码块 with中代码块执行完毕时执行我啊 <class 'attributeerror'> ***着火啦,救火啊*** <traceback object at 0x1062ab048> 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
class open: def __init__(self, filepath, mode='r', encoding='utf-8'): self.filepath = filepath self.mode = mode self.encoding = encoding def __enter__(self): # print('enter') self.f = open(self.filepath, mode=self.mode, encoding=self.encoding) return self.f def __exit__(self, exc_type, exc_val, exc_tb): # print('exit') self.f.close() return true def __getattr__(self, item): return getattr(self.f, item) with open('a.txt', 'w') as f: print(f) f.write('aaaaaa') f.wasdf #抛出异常,交给__exit__处理
<_io.textiowrapper name='a.txt' mode='w' encoding='utf-8'>
使用with语句的目的就是把代码块放入with中执行,with结束后,自动完成清理工作,无须手动干预
在需要管理一些资源比如文件,网络连接和锁的编程环境中,可以在__exit__中定制自动释放资源的机制,你无须再去关系这个问题,这将大有用处
如对本文有疑问, 点击进行留言回复!!
网友评论