标签:开发 ble 实现 views 临时 toolbar ora 上传 技术
1
2
|
with open ( ‘test.txt‘ ) as f: print f.readlines() |
1
2
|
with EXPR as VAR: BLOCK |
01
02
03
04
05
06
07
08
09
10
11
12
|
class Resource(): def __enter__( self ): print ( ‘===connect to resource===‘ ) return self def __exit__( self , exc_type, exc_val, exc_tb): print ( ‘===close resource connection===‘ ) def operate( self ): print ( ‘===in operation===‘ ) with Resource() as res: res.operate() |
01
02
03
04
05
06
07
08
09
10
11
12
13
14
|
class Resource(): def __enter__( self ): print ( ‘===connect to resource===‘ ) return self def __exit__( self , exc_type, exc_val, exc_tb): print ( ‘===close resource connection===‘ ) return True def operate( self ): 1 / 0 with Resource() as res: res.operate() |
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
|
import contextlib @contextlib .contextmanager def open_func(file_name): # __enter__方法 print ( ‘open file:‘ , file_name, ‘in __enter__‘ ) file_handler = open (file_name, ‘r‘ ) # 【重点】:yield yield file_handler # __exit__方法 print ( ‘close file:‘ , file_name, ‘in __exit__‘ ) file_handler.close() return with open_func( ‘/Users/MING/mytest.txt‘ ) as file_in: for line in file_in: print (line) |
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
import contextlib @contextlib .contextmanager def open_func(file_name): # __enter__方法 print ( ‘open file:‘ , file_name, ‘in __enter__‘ ) file_handler = open (file_name, ‘r‘ ) try : yield file_handler except Exception as exc: # deal with exception print ( ‘the exception was thrown‘ ) finally : print ( ‘close file:‘ , file_name, ‘in __exit__‘ ) file_handler.close() return with open_func( ‘/Users/MING/mytest.txt‘ ) as file_in: for line in file_in: 1 / 0 print (line) |
标签:开发 ble 实现 views 临时 toolbar ora 上传 技术
原文地址:https://www.cnblogs.com/heimaguangzhou/p/11549993.html