Python3之 contextlib

Python中当我们们打开文本时,通常会是用with语句,with语句允许我们非常方便的使用资源,而不必担心资源没有关闭。

1
2
with  open ( '/path/filename' 'r' ) as f:
     f.read()

  然而,并不是只有open()函数返回fp对象才能使用 with 语句。实际上,任何对象,只要正确实现上下文管理,就可以使用with语句。实现上下文管理是通过 __enter__ 和 __exit__ 这两个方法实现的。例如,下面的class实现了这两个方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class  Query( object ):
 
     def  __init__( self , name):
         self .name  =  name
 
     def  __enter__( self ):
         print ( 'Begin' )
         return  self
 
     def  __exit__( self , exc_type, exc_value, traceback):
         if  exc_type:
             print ( 'Error' )
         else :
             print ( 'End' )
 
     def  query( self ):
         print ( 'Query info about %s...'  %  self .name)

  这样我们可以把自己写的资源对象用于 with 语句。

1
2
with Query( 'Bob' ) as q:
     q.query()

  

@contextmanager

  编写 __enter__ 和 __exit__ 仍然很繁琐,因此Python的标准库 contextlib 提供了更简单的写法,上面的代码可以改写为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
from  contextlib  import  contextmanager
 
class  Query( object ):
 
     def  __init__( self , name):
         self .name  =  name
 
     def  query( self ):
         print ( 'Query info about %s...'  %  self .name)
 
@contextmanager
def  create_query(name):
     print ( 'Begin' )
     =  Query(name)
     yield  q
     print ( 'End' )

  @contextmanager 这个装饰器接受一个 generator,用 yield 语句把 with ... as var 把变量输出出去,然后,with 语句就可以正常的工作了:

1
2
with create_query( 'Bob' ) as q:
     q.query()

  很多时候,我们希望在某段代码执行前后自动执行特定代码,也可以用 @contextmanager实现。

1
2
3
4
5
6
7
8
9
@contextmanager
def  tag(name):
     print ( "<%s>"  %  name)
     yield
     print ( "</%s>"  %  name)
 
with tag( "h1" ):
     print ( "hello" )
     print ( "world" )

  上述代码执行结果:

1
2
3
4
<h1>
hello
world
< / h1>

  代码的执行顺序是:

  1.  with 语句 首先执行 yield 之前的语句,因此打印出 <h1>.
  2.  yield 调用会执行 with 语句内部的所有语句,因此打印出 hello 和 world.
  3.  最后执行yield之后的语句,打印出 </h1>.

 

@closing

  如果一个对象没有实现上下文,就不能使用 with 语句,但是可以用 closing() 来把对象变为上下文对象。

1
2
3
4
5
6
from  contextlib  import  closing
from  urllib.request  import  urlopen
 
with closing(urlopen( 'https://www.python.org' )) as page:
     for  line  in  page:
         print (line)

  closing 也是一个经过 @contextmanager 装饰的generator

1
2
3
4
5
6
@contextmanager
def  closing(thing):
     try :
         yield  thing
     finally :
         thing.close()

  它的作用就是把任意对象变为上下文对象,并支持 with语句。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值