python with as

参考:http://zhoutall.com/archives/325

开了文件就得关,

 

用 with as 就省了 finally

try:
    with open( "a.txt" ) as f :
        do something
except xxxError:
    do something about exception

with-as表达式极大的简化了每次写finally的工作,这对保持代码的优雅性是有极大帮助的。

posted @ 2016-03-24 11:27  Pudding_AI  阅读(143)  评论(0编辑  收藏  举报