Python 中with 使用说明

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语句,代码如下:


1

2

3

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


1

2

3

4

5

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版本的代码:


1

2

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


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

#!/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: 运行代码,输出如下


1

2

3

4

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


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

#!/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: 代码执行后:


1

2

3

4

5

6

7

8

9

10

bash-3.2$ ./with_example02.py

type: <type ‘exceptions.ZeroDivisionError‘>

value: integer division or modulo by zero

trace: <traceback object at 0x1004a8128>

Traceback (most recent call last):

  File "./with_example02.py", line 19, in <module>

    sample.do_something()

  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上面找到。

时间: 2024-11-03 05:35:35

Python 中with 使用说明的相关文章

Python中关于导入(import)语句的使用说明

在所有的语言中,语言的开发者都会为我们提供强大的库文件供我们调用,通过使用这些库文件,我们才可以把程序完美的运行起来,既然要用到库文件,我们就要导入我们需要使用的库文件,因为,机器是不知道我们想基于哪些库进行程序的编写的.在导入库的时候,不同的语言有自己的风格.例如在C/C++等语言中,我们使用#include <>或者#include ""来导入库或者头文件.那么在python中我们怎么导入库呢. 首先,在Python中,我们可以导入三种库:Python标准模块,Pyth

python Flask EasyUI使用说明

0.前言 本文说明如何在flask框架下使用前端组件EasyUI.在flask框架中链接css文件,js脚本和图片等静态文件的方式和其他web服务器存在差别.在flask框架中这些静态文件一般存放与static文件夹中,并通过url_for函数指定static中相对位置和文件名. [代码仓库] 代码仓库位于bitbucket--flask-easyui,请使用支持HTML5的浏览器打开链接. [相关博文] [1]python 扩展库安装 使用第三方镜像源 [2]python Flask 学前班

Python中如何Getting Help

在Python中Gettting Help有如下两种方法: 1 使用dir函数,dir的参数可以是一个真正的对象实例,也可以是一个数据类型,无论哪种情形,dir函数都返回与这个对象或者数据类型相关联的所有方法 2 使用help,help函数的参数可以是一个数据类型,也可以是一个数据类型.方法,前者会列举出这个数据类型相关联的所有方法以及方法的使用说明,后者只列举出指定方法的使用说明 原文地址:https://www.cnblogs.com/chaoguo1234/p/9127714.html

python中字符串的操作方法

python中字符串的操作方法大全 更新时间:2018年06月03日 10:08:51 作者:骏马金龙 我要评论这篇文章主要给大家介绍了关于python中字符串操作方法的相关资料,文中通过示例代码详细介绍了关于python中字符串的大小写转换.isXXX判断.填充.子串搜索.替换.分割.join以及修剪:strip.lstrip和rstrip的相关内容,需要的朋友可以参考下 前言 python中字符串对象提供了很多方法来操作字符串,功能相当丰富.?123 print(dir(str)) [...

Python中的测试工具

??当我们在写程序的时候,我们需要通过测试来验证程序是否出错或者存在问题,但是,编写大量的测试来确保程序的每个细节都没问题会显得很繁琐.在Python中,我们可以借助一些标准模块来帮助我们自动完成测试过程,比如: unittest: 一个通用的测试框架: doctest: 一个更简单的模块,是为检查文档而设计的,但也非常适合用来编写单元测试. ??下面,笔者将会简单介绍这两个模块在测试中的应用. doctest ??doctest模块会搜索那些看起来像是python交互式会话中的代码片段,然后尝

走入计算机的第四十天(python中sockserver模块)

一.Python中的sockserver模块 1.该模块与sock模块不同之处是该模块自动帮我们分装好了一些功能,让我们在编程的时候直接调用这些功能就可以了,节省了编程步骤. 2.如图所示 注释:上图为服务端设置 该模块的操作方法比较死板,我们只要会熟悉的使用他就可以了.

python中if __name__ == &#39;__main__&#39;:

Using a module's __name__ Example? 8.2.? Using a module's __name__ #!/usr/bin/python # Filename: using_name.py if __name__ == '__main__': print 'This program is being run by itself' else: print 'I am being imported from another module' Output $ pytho

关于Python中的yield

关于Python中的yield http://www.cnblogs.com/tqsummer/archive/2010/12/27/1917927.html http://www.ibm.com/developerworks/cn/opensource/os-cn-python-yield/ 一.迭代器(iterator) 在Python中,for循环可以用于Python中的任何类型,包括列表.元祖等等,实际上,for循环可用于任何“可迭代对象”,这其实就是迭代器 迭代器是一个实现了迭代器协议

python中的那些“神器”

"武林至尊,宝刀屠龙,号令天下,莫敢不从,倚天不出,谁与争锋",这是神器.不过今天要说的python中的"神器"就没有这么厉害了,这里要说的"神器"其实就是名称里面带了个"器"的,如下: 列表解析器 迭代器 生成器 装饰器 列表解析器 现在遇到了这样一个问题需要解决:"有一个数字的列表,要求对该列表中的奇数乘以2,返回处理完成后的列表(不改变原来列表的顺序,仅对列表中的奇数乘以2)",比较传统的方法可能会是