Python中with的用法

第一次遇到with是在文件那一章,with的作用是就是会自动关掉文件管道。

with open('path','读写模式‘) as f:
    do something

这一部分就等价于

f = open('path','读写模式')
do something
f.close()

第二次是在数据库连接,连接池那里。使用的基本思想大致是with所求值的对象必须有一个enter()方法和一个exit()方法。下面给一个简单的例子去说明使用with的时候做了哪些操作

class Sample:
    def __enter__(self):
        print "In __enter__()"
        return "Foo"
    def __exit__(self, type,value, trace):
        print "In__exit__()"
    
    def get_sample():
        return Sample()
with get_sample() as sample:
    print(sample)
  1. with开始,enter()方法被执行
  2. enter()方法返回的值 - 这个例子中是Foo,赋值给变量sample
  3. 执行代码块,打印变量sample的值为 Foo
  4. exit()方法被调用 with真正强大之处是它可以处理异常。注意到Sample类的exit方法有三个参数- val, typetrace。 这些参数在异常处理中相当有用。
class Sample:
    def __enter__(self):
        return self
    def __exit__(self, exc_type, exc_val, exc_tb):
        print(exc_type)
        print(exc_val)
        print(exc_tb)
    def doSomething(self):
        a = 1/0
        return a
    def getSample():
        return Sample()
if __name__ == '__main__':
    with getSample() as sample:
        sample.doSomething()

这段代码的执行结果是

/usr/bin/python3.5 /home/zionhuang/data/awesome-python3-webapp/test/test8.py
<class 'ZeroDivisionError'>
division by zero
<traceback object at 0x7fbb5c091d48>
Traceback (most recent call last):
File "/home/zionhuang/data/awesome-python3-webapp/test/test8.py", line 23, in <module>
sample.doSomething()
File "/home/zionhuang/data/awesome-python3-webapp/test/test8.py", line 14, in doSomething
a = 1/0
ZeroDivisionError: division by zero
Process finished with exit code 1

结果头三句加黑的分别是val, typetrace这三个参数在出现异常时的值
常抛出时,与之关联的typevaluestack trace传给exit()方法,因此抛出的ZeroDivisionError异常被打印出来了。开发库时,清理资源,关闭文件等等操作,都可以放在exit方法当中。
因此,Pythonwith语句是提供一个有效的机制,让代码更简练,同时在异常产生时,清理工作更简单。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容