使用语言的好特性,而不是那些糟糕的特性————不知道谁说的
好久不学习python的语法了,上次去面试,和面试官聊到了python中的with-as statement(也称context manager),挺感兴趣的,这两天学习了一番,收获颇丰在此分享。
先说明一个常见问题,文件打开:
1
2
3
4
5
6
7
|
try : f = open ( 'xxx' ) do something except : do something finally : f.close() |
其实我个人不止一次在网上看到有这么写的了,这个是错的。
首先正确的如下:
1
2
3
4
5
6
7
8
9
10
11
|
try : f = open ( 'xxx' ) except : print 'fail to open' exit( - 1 ) try : do something except : do something finally : f.close() |
很麻烦不是么,但正确的方法就是这么写。
我们为什么要写finally,是因为防止程序抛出异常最后不能关闭文件,但是需要关闭文件有一个前提就是文件已经打开了。
在第一段错误代码中,如果异常发生在f=open(‘xxx’)的时候,比如文件不存在,立马就可以知道执行f.close()是没有意义的。改正后的解决方案就是第二段代码。
好了言归正转,开始讨论with语法。
首先我们从下面这个问题谈起,try-finally的语法结构:
1
2
3
4
5
|
set things up try : do something finally : tear things down |
这东西是个常见结构,比如文件打开,set things up
就表示f=open('xxx')
,tear things down
就表示f.close()
。在比如像多线程锁,资源请求,最终都有一个释放的需求。Try…finally结构保证了tear things down这一段永远都会执行,即使上面do something得工作没有完全执行。
如果经常用这种结构,我们首先可以采取一个较为优雅的办法,封装!
1
2
3
4
5
6
7
8
9
10
11
|
def controlled_execution(callback): set things up try : callback(thing) finally : tear things down def my_function(thing): do something controlled_execution(my_function) |
封装是一个支持代码重用的好办法,但是这个办法很dirty,特别是当do something中有修改一些local variables的时候(变成函数调用,少不了带来变量作用域上的麻烦)。
另一个办法是使用生成器,但是只需要生成一次数据,我们用for-in结构去调用他:
1
2
3
4
5
6
7
8
9
|
def controlled_execution(): set things up try : yield thing finally : tear things down for thing in controlled_execution(): do something with thing |
因为thing只有一个,所以yield语句只需要执行一次。当然,从代码可读性也就是优雅的角度来说这简直是糟糕透了。我们在确定for循环只执行一次的情况下依然使用了for循环,这代码给不知道的人看一定很难理解这里的循环是什么个道理。
最终的python-dev团队的解决方案。(python 2.5以后增加了with表达式的语法)
1
2
3
4
5
6
7
8
9
|
class controlled_execution: def __enter__( self ): set things up return thing def __exit__( self , type , value, traceback): tear things down with controlled_execution() as thing: do something |
在这里,python使用了with-as的语法。当python执行这一句时,会调用__enter__函数,然后把该函数return的值传给as后指定的变量。之后,python会执行下面do
something的语句块。最后不论在该语句块出现了什么异常,都会在离开时执行__exit__。
另外,__exit__除了用于tear things down,还可以进行异常的监控和处理,注意后几个参数。要跳过一个异常,只需要返回该函数True即可。下面的样例代码跳过了所有的TypeError,而让其他异常正常抛出。
1
2
|
def __exit__( self , type , value, traceback): return isinstance (value, TypeError) |
在python2.5及以后,file对象已经写好了__enter__和__exit__函数,我们可以这样测试:
1
2
3
4
5
6
7
8
9
10
11
12
|
>>> f = open ( "x.txt" ) >>> f < open file 'x.txt' , mode 'r' at 0x00AE82F0 > >>> f.__enter__() < open file 'x.txt' , mode 'r' at 0x00AE82F0 > >>> f.read( 1 ) 'X' >>> f.__exit__( None , None , None ) >>> f.read( 1 ) Traceback (most recent call last): File "<stdin>" , line 1 , in <module> ValueError: I / O operation on closed file |
之后,我们如果要打开文件并保证最后关闭他,只需要这么做:
1
2
3
|
with open ( "x.txt" ) as f: data = f.read() do something with data |
如果有多个项,我们可以这么写:
1
2
|
with open ( "x.txt" ) as f1, open ( 'xxx.txt' ) as f2: do something with f1,f2 |
上文说了__exit__函数可以进行部分异常的处理,如果我们不在这个函数中处理异常,他会正常抛出,这时候我们可以这样写(python 2.7及以上版本,之前的版本参考使用contextlib.nested这个库函数):
1
2
3
4
5
|
try : with open ( "a.txt" ) as f : do something except xxxError: do something about exception |
总之,with-as表达式极大的简化了每次写finally的工作,这对保持代码的优雅性是有极大帮助的。
There are two annoying things here. First, you end up forgetting to close the file handler. The second is how to handle exceptions that may occur once the file handler has been obtained. One could write something like this to get around this: 这里有两个问题。一是可能忘记关闭文件句柄;二是文件读取数据发生异常,没有进行任何处理。下面是处理异常的加强版本:
While this works well, it is unnecessarily verbose. This is where with is useful. The good thing about with apart from the better syntax is that it is very good handling exceptions. The above code would look like this, when using with: 虽然这段代码运行良好,但是太冗长了。这时候就是with一展身手的时候了。除了有更优雅的语法,with还可以很好的处理上下文环境产生的异常。下面是with版本的代码:
with如何工作?
while this might look like magic, the way Python handles with is more clever than magic. The basic idea is that the statement after with has to evaluate an object that responds to an __enter__() as well as an __exit__() function. 这看起来充满魔法,但不仅仅是魔法,Python对with的处理还很聪明。基本思想是with所求值的对象必须有一个__enter__()方法,一个__exit__()方法。 After the statement that follows with is evaluated, the __enter__() function on the resulting object is called. The value returned by this function is assigned to the variable following as. After every statement in the block is evaluated, the __exit__() function is called. 紧跟with后面的语句被求值后,返回对象的__enter__()方法被调用,这个方法的返回值将被赋值给as后面的变量。当with后面的代码块全部被执行完之后,将调用前面返回对象的__exit__()方法。 This can be demonstrated with the following example: 下面例子可以具体说明with如何工作:
When executed, this will result in: 运行代码,输出如下
As you can see, The __enter__() function is executed The value returned by it - in this case "Foo" is assigned to sample The body of the block is executed, thereby printing the value of sample ie. "Foo" The __exit__() function is called. What makes with really powerful is the fact that it can handle exceptions. You would have noticed that the __exit__() function for Sample takes three arguments - val, type and trace. These are useful in exception handling. Let’s see how this works by modifying the above example. 正如你看到的, 1. __enter__()方法被执行 2. __enter__()方法返回的值 - 这个例子中是"Foo",赋值给变量'sample' 3. 执行代码块,打印变量"sample"的值为 "Foo" 4. __exit__()方法被调用 with真正强大之处是它可以处理异常。可能你已经注意到Sample类的__exit__方法有三个参数- val, type 和 trace。 这些参数在异常处理中相当有用。我们来改一下代码,看看具体如何工作的。
Notice how in this example, instead of get_sample(), with takes Sample(). It does not matter, as long as the statement that follows with evaluates to an object that has an __enter__() and __exit__() functions. In this case, Sample()’s __enter__() returns the newly created instance of Sample and that is what gets passed to sample. 这个例子中,with后面的get_sample()变成了Sample()。这没有任何关系,只要紧跟with后面的语句所返回的对象有__enter__()和__exit__()方法即可。此例中,Sample()的__enter__()方法返回新创建的Sample对象,并赋值给变量sample。 When executed: 代码执行后:
Essentially, if there are exceptions being thrown from anywhere inside the block, the __exit__() function for the object is called. As you can see, the type, value and the stack trace associated with the exception thrown is passed to this function. In this case, you can see that there was a ZeroDivisionError exception being thrown. People implementing libraries can write code that clean up resources, close files etc. in their __exit__() functions. 实际上,在with后面的代码块抛出任何异常时,__exit__()方法被执行。正如例子所示,异常抛出时,与之关联的type,value和stack trace传给__exit__()方法,因此抛出的ZeroDivisionError异常被打印出来了。开发库时,清理资源,关闭文件等等操作,都可以放在__exit__方法当中。 Thus, Python’s with is a nifty construct that makes code a little less verbose and makes cleaning up during exceptions a bit easier. 因此,Python的with语句是提供一个有效的机制,让代码更简练,同时在异常产生时,清理工作更简单。