With语句是什么?

Python’s with statement provides a very convenient way of dealing with the situation where you have to do a setup and teardown to make something happen. A very good example for this is the situation where you want to gain a handler to a file, read data from the file and the close the file handler.

有一些任务,可能事先需要设置,事后做清理工作。对于这种场景,Python的with语句提供了一种非常方便的处理方式。一个很好的例子是文件处理,你需要获取一个文件句柄,从文件中读取数据,然后关闭文件句柄。

Without the with statement, one would write something along the lines of:

如果不用with语句,代码如下:

file = open("/tmp/foo.txt")
data = file.read()
file.close()

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:

这里有两个问题。一是可能忘记关闭文件句柄;二是文件读取数据发生异常,没有进行任何处理。下面是处理异常的加强版本:

file = open("/tmp/foo.txt")
try:
data = file.read()
finally:
file.close()

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 open("/tmp /foo.txt") as file:
data = file.read()

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如何工作:

#!/usr/bin/env python
# with_example01.py 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:", sample

When executed, this will result in:

行代码,输出如下

bash-3.2$ ./with_example01.py
In __enter__()
sample: Foo
In __exit__()

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.

正如你看到的,

enter()方法被执行
enter()方法返回的值 - 这个例子中是"Foo",赋值给变量'sample'
执行代码块,打印变量"sample"的值为 "Foo"
exit()方法被调用
with真正强大之处是它可以处理异常。可能你已经注意到Sample类的__exit__方法有三个参数- val, type 和 trace。 这些参数在异常处理中相当有用。我们来改一下代码,看看具体如何工作的。

#!/usr/bin/env python
# with_example02.py class Sample:
def __enter__(self):
return self def __exit__(self, type, value, trace):
print "type:", type
print "value:", value
print "trace:", trace def do_something(self):
bar = 1/0
return bar + 10 with Sample() as sample:
sample.do_something()

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:

代码执行后:

bash-3.2$ ./with_example02.py
type:
value: integer division or modulo by zero
trace:
Traceback (most recent call last):
File "./with_example02.py", line 19, in
sample.do_somet hing()
File "./with_example02.py", line 15, in do_something
bar = 1/0
ZeroDivisionError: integer division or modulo by zero

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语句是提供一个有效的机制,让代码更简练,同时在异常产生时,清理工作更简单。

I have put the code examples given here on Github.

示例代码可以在Github上面找到。

参考引用:

http://linbo.github.io/2013/01/08/python-with

[翻译]Python with 语句的更多相关文章

  1. StackOverFlow排错翻译 - Python字符串替换: How do I replace everything between two strings without replacing the strings?

    StackOverFlow排错翻译 - Python字符串替换: How do I replace everything between two strings without replacing t ...

  2. Python —条件语句

    条件语句 Python条件语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代码块. 可以通过下图来简单了解条件语句的执行过程: Python程序语言指定任何非0和非空(null ...

  3. [翻译]PYTHON中如何使用*ARGS和**KWARGS

    [翻译]Python中如何使用*args和**kwargs 函数定义 函数调用 不知道有没有人翻译了,看到了,很短,顺手一翻 原文地址 入口 或者可以叫做,在Python中如何使用可变长参数列表 函数 ...

  4. Python pass 语句使用示例

    Python pass 语句的使用方法示例.Python pass是空语句,pass语句什么也不做,一般作为占位符或者创建占位程序,是为了保持程序结构的完整性,pass语句不会执行任何操作,比如: P ...

  5. Python学习教程(learning Python)--1.2.1 Python输出语句print基本使用

    Python提供很多的内建(built-in)函数,使用者可以不用自己写代码就可以完成一个功能很强大的程序, 在Python里使用最多的(也许是)print函数主要用于用户输出信息. 基本用法:pri ...

  6. Python 条件语句

    Python条件语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代码块. Python程序语言指定任何非0和非空(null)值为true,0 或者 null为false. Py ...

  7. python 循环语句 函数 模块

    python循环语句 while循环语法结构 当需要语句不断的重复执行时,可以使用while循环 while expression: while_suite 语句ehile_suite会被连续不断的循 ...

  8. jmeter数据库,charles抓包,Python循环语句

    jmeter数据库,charles抓包,Python循环语句 一.Jemeter数据库 添加jar包数据库 jemeter=>浏览 添加JDBC Connection Configuration ...

  9. Python import语句导入模块语法[转]

    Python import语句导入模块语法 社区推荐:掘金是国内最活跃的技术社区,我们每日有优质Python开发实例分享,海量python开源库推送.来掘金,和更多懂技术的小伙伴交流.   pytho ...

随机推荐

  1. 在Sublime Text 3中配置编译和运行C++程序

    下载解压MinGW至目标目录,本次安装的解压目录为C:\MinGW 设置环境变量.右击我的电脑,属性-->高级-->环境变量. 在系统环境变量PATH里添加C:\MinGW\bin(如果里 ...

  2. 九九乘法口诀引申出NN乘法口诀

    package com.tfj.function; import java.util.Scanner; public class NNTable { public void method1(int n ...

  3. API函数

    1. API之网络函数 WNetAddConnection 创建同一个网络资源的永久性连接 WNetAddConnection2 创建同一个网络资源的连接 WNetAddConnection3 创建同 ...

  4. NOI2011道路修建

    2435: [Noi2011]道路修建 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1974  Solved: 550[Submit][Status ...

  5. CodeForces 450

    A - Jzzhu and Children Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & % ...

  6. Android Matrix用法

    Matrix,中文里叫矩阵,高等数学里有介绍,在图像处理方面,主要是用于平面的缩放.平移.旋转等操作. 首先介绍一下矩阵运算.加法和减法就不用说了,太简单了,对应位相加就好.图像处理,主要用到的是乘法 ...

  7. HDU-5414 CRB and String

    http://acm.hdu.edu.cn/showproblem.php?pid=5414 题意:给定字符串s和t,可以在s里面选一个字符c,然后任选一个字符d(d!=c)将d插入到c的后面,问能不 ...

  8. 终于写好了SR4000的一个实用类了

    /*----------------------------------------------------------------------------- *   *   版权声明: *   可以 ...

  9. while MyJob = '程序员' do --- 序

    因为自己的际遇,忽然想写点什么留在这个世上.也许只是想证明自己活过吧. 所以,这不会是一个过去时的小说,这将是一个接近进行时的记叙.之所以是接近,因为我只有在空余时间,才能记录最近的经历.出于保护隐私 ...

  10. Java笔记(六)……程序流程控制

    判断结构 三种结构: 1: if(条件表达式) 2: { 3: 执行语句; 4: } 5:  6: if(条件表达式) 7: { 8: 执行语句; 9: } 10: else 11: { 12: 执行 ...