文档章节

ZZ:Understanding Python's "with" statement

J
 JMARS
发布于 2014/01/09 15:53
字数 572
阅读 11
收藏 0

Judging from comp.lang.python and other forums, Python 2.5’s new with statement seems to be a bit confusing even for experienced Python programmers.

As most other things in Python, the with statement is actually very simple, once you understand the problem it’s trying to solve. Consider this piece of code:

    set things up
    try:
        do something
    finally:
        tear things down

Here, “set things up” could be opening a file, or acquiring some sort of external resource, and “tear things down” would then be closing the file, or releasing or removing the resource. The try-finally construct guarantees that the “tear things down” part is always executed, even if the code that does the work doesn’t finish.

If you do this a lot, it would be quite convenient if you could put the “set things up” and “tear things down” code in a library function, to make it easy to reuse. You can of course do something like

    def controlled_execution(callback):
        set things up
        try:
            callback(thing)
        finally:
            tear things down

    def my_function(thing):
        do something

    controlled_execution(my_function)

But that’s a bit verbose, especially if you need to modify local variables. Another approach is to use a one-shot generator, and use the for-in statement to “wrap” the code:

    def controlled_execution():
        set things up
        try:
            yield thing
        finally:
            tear things down

    for thing in controlled_execution():
        do something with thing

But yield isn’t even allowed inside a try-finally in 2.4 and earlier. And while that could be fixed (and it has been fixed in 2.5), it’s still a bit weird to use a loop construct when you know that you only want to execute something once.

So after contemplating a number of alternatives, GvR and the python-dev team finally came up with a generalization of the latter, using an object instead of a generator to control the behaviour of an external piece of code:

    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:
         some code

Now, when the “with” statement is executed, Python evaluates the expression, calls the __enter__ method on the resulting value (which is called a “context guard”), and assigns whatever __enter__ returns to the variable given by as. Python will then execute the code body, and no matter what happens in that code, call the guard object’s __exit__ method.

As an extra bonus, the __exit__ method can look at the exception, if any, and suppress it or act on it as necessary. To suppress the exception, just return a true value. For example, the following __exit__ method swallows any TypeError, but lets all other exceptions through:

    def __exit__(self, type, value, traceback):
        return isinstance(value, TypeError)

In Python 2.5, the file object has been equipped with __enter__ and __exit__ methods; the former simply returns the file object itself, and the latter closes the file:

    >>> 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

so to open a file, process its contents, and make sure to close it, you can simply do:

with open("x.txt") as f:
    data = f.read()
    do something with data

This wasn’t very difficult, was it?


本文转载自:http://effbot.org/zone/python-with-statement.htm

J
粉丝 1
博文 5
码字总数 0
作品 0
海淀
私信 提问
Python中如何使用yield,对于庞大迭代的优化处理

一直困扰于yield的使用,今天看到一篇不错的解释,虽然都是英文,不过不要紧,可以跳开,直接看代码的部分就能懂 Improve Your Python: 'yield' and Generators Explained Posted on Apr 07...

青鸾之旅
2013/08/02
0
3
Your Guide to Python with MLSQL Stack (二)

In the previous post Your Guide to NLP with MLSQL Stack (一), we already have known how to build a RandomForest model to classify text content. The TF/IDF, RandomForest are all......

祝威廉
05/13
0
0
Python Tips, Tricks, and Hacks

一、快速技巧 1.1、4 种引号 ' ''' " """ print """I wish that I'd never heard him say, '''She said, "He said, 'Give me five dollars'"'''""" 1.2、对象/变量的真与假 my_object ...

大数据之路
2013/07/23
0
0
Python2.7 linecache&&timeit模块学习

linecache 当你试图优化从任何文件中读取任何行时,可以使用linecache模块,该模块采用cache缓存文件, 你可以在缓存中读取指定文件的指定行。 linecache提供了以下函数: linecache.getlin...

China_OS
2013/01/31
0
0
Understanding Java Lambdas

Understanding Java Lambdas Posted on 2017-04-25 It took me quite some reading and coding to finally understand how Java Lambdas actually work conceptually. Most tutorials and in......

祖冲之
2018/08/02
0
0

没有更多内容

加载失败,请刷新页面

加载更多

springboot配置百度UEditor 富文本详解

富文本简介 UEditor是由百度web前端研发部开发所见即所得富文本web编辑器,具有轻量,可定制,注重用户体验等特点,开源基于MIT协议,允许自由使用和修改代码... 准备工作 ueditor需要单独文...

wotrd
昨天
1
0
mysql 5.7之my.cnf配置大全

[client]port = 3306socket = /tmp/mysql.sock[mysqld]###############################基础设置######################################Mysql服务的唯一编号 每个mysql服务...

Online_Reus
昨天
2
0
MAVEN打包时引入外部链接的包

1.项目引入了ORACLE的jar包,MAVEN配置如下 2.打jar包的时候需要指定下main入口函数mainClass <dependency> <groupId>com.oracle</groupId> <artifactId>ojdbc6</artifactId> ......

Cobbage
昨天
1
0
rror: Default interface methods are only supported starting with Android N (--min-api 24): java.io.

项目运行的时候,如果报错 Error: Default interface methods are only supported starting with Android N (--min-api 24): java.io.InputStream org.apache.poi.sl.usermodel.ObjectShape.......

chenhongjiang
昨天
2
0
聊聊spring cloud openfeign的Targeter

序 本文主要研究一下spring cloud openfeign的Targeter Targeter spring-cloud-openfeign-core-2.2.0.M1-sources.jar!/org/springframework/cloud/openfeign/Targeter.java interface Targe......

go4it
昨天
2
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部