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

时间:2022-07-31 00:15:52
 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