转自点击打开链接

《python标准库》上这么一句话:

with open('filename', 'wt') as f:f.write('hello, world!')

我不明白为什么这样写,下面这篇文章对此做出了解释

原文地址:http://python.42qu.com/11155501

------------------

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上面找到。译注:本文原文见Understanding Python's "With" Statement

理解python的with as 语句相关推荐

  1. python中if语句怎么结束_if语句,让你透彻理解python中的if语句(限小白)

    传统化交易还是有些人性无法避免的弊端,这个时候,我们就要借助计算机进行程序化交易,让我们的交易走得更高更远.而程序化交易必备的知识是Python,下面我们讲解下python中的if语句. 只要你耐心看 ...

  2. 深入理解Python中的if语句

    公众号:尤而小屋 作者:Peter 编辑:Peter 大家好,我是Peter~ 在生活中总是会听到这样的话: 如果我上课认真一点,英语肯定可以及格 如果我努力锻炼,肯定可以减到100斤 如果我技术过硬 ...

  3. 深入理解Python的With-as语句

    学习Python有一段时间了,最近做一个项目会涉及到文件的读取和关闭.比如:我想把一些对象序列化到文件里面,然后当我再次使用的时候,在从文件里面读取反序列化成对象.像这种操作一般都是用try-exce ...

  4. python语句解释_深入理解python with 语句

    深入理解python with 语句 python中with 语句作为try/finally 编码范式的一种替代, 适用于对资源进行访问的场合,确保不管使用过程中是否发生异常都会执行必要的" ...

  5. python中return的理解-Python return语句 函数返回值

    return语句是从python 函数返回一个值,在讲到定义函数的时候有讲过,每个函数都要有一个返回值.Python中的return语句有什么作用,今天就来仔细的讲解一下. python 函数返回值 ...

  6. 如何用python制作九九乘法表_“九九乘法表”案例理解python循环语句

    这是树哥讲python的第七篇文章. 在所有的编程语言中,循环语句是最常用的语句之一,因为循环语句可以充分发挥计算机运算速度优点,很多人做起来无法忍受的重复,对于计算机来讲轻松完成.所以今天,我们就是 ...

  7. Python中的 if 语句简单介绍,中英文完整理解

    来学习Python中的if语句,用英文如何表达?看看else究竟是留还是省略? Python 中的if语句可以省略else吗?if 后面的表达式求值结果是什么类型?

  8. python装饰器原理-深刻理解python装饰器

    我们要完全理解python装饰器,不是很容易,主要归结有如下困难: 1. 关于函数"变量"(或"变量"函数)的理解 2. 关于高阶函数的理解 3. 关于嵌套函数 ...

  9. python怎么导入包-如何理解Python中包的引入

    Python的from import *和from import *,它们的功能都是将包引入使用,但是它们是怎么执行的以及为什么使用这种语法呢? 从一模块导入全部功能 from import * me ...

最新文章

  1. 程序计算机限制无法打开,由于一个软件限制策略的阻止,windows无法打开此程序...解决方案参考...
  2. 服务器论坛有哪些_SEO工作中,经常使用的无效外链有哪些?
  3. XmlValidatingReader
  4. PrimerCH4:表达式
  5. 使用 docker 编译运行 abp 项目
  6. .NET 的一点历史往事:和 Java 的恩怨
  7. shell 脚本实现退点输出
  8. php路由类默认模块,微擎入口路由及其模块入口路由 - YangJunwei
  9. [Algorithm]一切始于ADT-表达式计算
  10. 使用bootbox.js(二级务必提交书面和数字到数字中国)
  11. JIRA /mnt/server/atlassian-jira-6.3.6-standalone/bin/start-jira.sh
  12. Word中插入的Excle打开后闪退如何解决? - Office2019
  13. python gamma函数_Python 不完全伽马函数
  14. 91 个常见的 Laravel 面试题和答案
  15. UI设计规范-全文篇
  16. Linux一句话将文件夹的用户用户组设置为wps:wps
  17. 为什么要创业,有人盆满钵满、有人别无选择,区别在创业思维
  18. SEO人员,一定要为首页进行排名吗?
  19. Unity 碰撞检测
  20. 深度学习笔记之稀疏自编码器

热门文章

  1. 自由自在公司冰淇淋甜美的健康文化
  2. 【Python 爬虫】 1、爬虫基础概念
  3. 【收藏】生产订单业务流程
  4. 一个极好的ALV例子
  5. SAP 物料主数据屏幕增强
  6. OO实现ALV TABLE 八:ALV的布局功能
  7. “花钱矩阵”下共享单车的出路何在?
  8. InMobi收购美国Sprint旗下数据和广告公司Pinsight Media
  9. vscode怎么弄php,vscode如何设置语言
  10. getprocaddress得到为0_拼接图像得到全景图