首页 > 编程语言 > 详细

python with

时间:2014-11-06 10:44:34      阅读:218      评论:0      收藏:0      [点我收藏+]

三个代码等价。with还可以很好的处理上下文环境产生的异常。

1
2
3
file = open("/tmp/foo.txt")
data = file.read()
file.close()
1
2
3
4
5
file = open("/tmp/foo.txt")
try:
    data = file.read()
finally:
    file.close()
1
2
with open("/tmp/foo.txt") as file:
    data = file.read()

 

python with

原文:http://www.cnblogs.com/tina-ma/p/4077964.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!