VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > Python基础教程 >
  • Python with...as... 语法深入解析

with从Python 2.5就有,需要from __future__ import with_statement。自python 2.6开始,成为默认关键字。

也就是说with是一个控制流语句,跟if/for/while/try之类的是一类的,with可以用来简化try finally代码,看起来可以比try finally更清晰。

1
with EXPRESSION [ as VARIABLE] WITH-BLOCK

基本思想是with所求值的对象必须有一个__enter__()方法,一个__exit__()方法。

紧跟with后面的语句被求值后,返回对象的__enter__()方法被调用,这个方法的返回值将被赋值给as后面的变量。当with后面的代码块全部被执行完之后,将调用前面返回对象的__exit__()方法。

 

with expresion as variable的执行过程是,首先执行__enter__函数,它的返回值会赋给as后面的variable,想让它返回什么就返回什么,只要你知道怎么处理就可以了,如果不写as variable,返回值会被忽略。

然后,开始执行with-block中的语句,不论成功失败(比如发生异常、错误,设置sys.exit()),在with-block执行完成后,会执行__exit__函数。

这样的过程其实等价于:

1
2
3
4
5
try:
    执行 __enter__的内容
    执行 with_block.
finally:
    执行 __exit__内容

 

再看个例子

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

使用with...as...的方式替换,修改后的代码是:

1
2
with open("/tmp/foo.txt") as file:
    data = file.read()

    

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/usr/bin/env python
# with_example01.py
 
class Sample:
    def __enter__(self):
        print "In __enter__()"
        return "Foo"
 
    def __exit__(selftype, value, trace):
        print "In __exit__()"
        
def get_sample():
    return Sample()
with get_sample() as sample:
    print "sample:", sample

相关教程