Python with语句及context manager

with语句是对context manager(上下文管理者,以下简称CM)方法的包装,适用于对资源进行访问的场合,确保不管使用过程中是否发生异常都会执行必要的“清理”操作,释放资源,比如文件使用后自动关闭、线程中锁的自动获取和释放等。

with_item ::=  expression ["as" target]
    with-body
with open(‘x.txt’, mode=’w+’) as f:
f.write(‘xxxx\r\n’)

expression必须返回一个CM,CM的两个方法如下:

object.__enter__(self) 
object.__exit__(self, exc_type, exc_value, traceback) 

enter会在获取CM后调用,而exit会被存储并在with-body运行完成或者抛出异常后调用。

class withclass:
    def __init__(self):
        self.d = dict()

    def __enter__(self):
        print('do enter!')

    def __exit__(self, exc_type, exc_val, exc_tb):
        print('cleanup the resources.')
        return True

def with_t():
    with withclass() as wc:
        print('before exception!')
        raise Exception('A error occur!')
        print('after exception!')
    print('after with statement!')

if __name__ == '__main__':
    with_t()

输出为:

do enter!
before exception!
cleanup the resources.
after with statement!

可以看到抛出的异常被无视了,而资源也被成功释放;原因是抛出异常后进入exit代码并返回了True,而如果返回了False则会继续抛出产生的异常,现在我们修改exit的返回为False。

def __exit__(self, exc_type, exc_val, exc_tb):
        print('cleanup the resources.')
        return False

再看结果:

Traceback (most recent call last):
do enter!
  File "E:/QQSyncFolder/prj/mysite/pattern/with.py", line 30, in <module>
before exception!
    with_t()
cleanup the resources.
  File "E:/QQSyncFolder/prj/mysite/pattern/with.py", line 25, in with_t
    raise Exception('A error occur!')
Exception: A error occur!

可以发现,资源仍然被清除,但是Exception却被抛出来,可以被外层程序捕获处理。

总的而言,主要是为了简化try…except…finally的使用,同时使用with也避免了忘记在finally中释放资源的情况。
像threading,mutliprocessing中Lock,Condition,Semaphore等多线程,多进程通讯都实现了context manager接口。

def RLock:    
    __enter__ = acquire
    def __exit__(self, t, v, tb):
        self.release()

# 下面的语法多开心:)

with rlock_instance:
    # do sth

PS: contextlib module 提供了许多简单易用的context装饰器,请参考Python官方文档。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值