【3.12】contextlib简化上下文管理器

 1 #!/user/bin/env python
 2 # -*- coding:utf-8 -*-
 3 import contextlib
 4 
 5 
 6 @contextlib.contextmanager
 7 def file_open(file_name):
 8     print('file_open')
 9     yield
10     print('file end')
11 
12 
13 with file_open('zy.txt') as f_opened:
14     print('file processing')
file_open
file processing
file end

  

posted @ 2019-07-24 17:17  _simpleSmile  阅读(171)  评论(0编辑  收藏  举报