[zz]Understanding Python's "with" statement

refer to: http://effbot.org/zone/python-with-statement.htm

Judging from comp.lang.python and other forums, Python 2.5’s new withstatement (dead link) 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?

[zz]Understanding Python's "with" statement

时间: 2024-08-07 04:32:23

[zz]Understanding Python's "with" statement的相关文章

&lt;zz&gt;【Python运维】简单的Python运维脚本

from http://www.cnblogs.com/yixianclove/p/5824747.html 背景 最近在Windows 10上使用Linux子系统,发现它有一个非常坑爹的特点:Linux子系统是没有开机关机状态的,每次进入Bash shell就自动载入,退出后Linux子系统的所有进程都会被关闭,如果你撞了Mysql之类的服务要想随时运行的话就要保持Bash shell的随时开启,更坑的是这些服务并不会随之进入Bash shell而自动启动, 我只好写一个Python脚本用于管

理解python的with语句

有一些任务, 可能事先需要设置, 事后做清理工作. 对于这种场景, python的with语句提供了一种非常方变的处理方式, 一个很好的例子是文件处理. 你需要获取一个文件的句柄, 从文件中读取数据, 然后关闭文件句柄. 如果不用with语句, 代码如下: file = open("/tmp/foo.txt") data = file.read() file.close() 这里有两个比较烦人的地方, 一是可能忘记关闭句柄, 而是文件读取数据时发生异常, 却没有进行任何处理(文件读取数

理解Python中的with…as…语法

使用语言的好特性,而不是那些糟糕的特性————不知道谁说的 好久不学习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() 其

转: 理解Python的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 da

When to close cursors using MySQLdb

http://stackoverflow.com/questions/5669878/when-to-close-cursors-using-mysqldb I'm building a WSGI web app and I have a MySQL database. I'm using MySQLdb, which provides cursors for executing statements and getting results. What is the standard pract

【转】 Python3 With as 语句如何理解

< python 标准库 >上这么一句话: [python] view plain copy 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

Python 学习资源汇总

推荐书籍: Python核心编程(第二版)(强烈推荐,建议有一定基础的看,或者看完简明Python教程再看) Python 基础教程 第二版 (入门,没有核心编程好,但也不错) 编写高质量代码:改善Python程序的91个建议 (进阶,有一定基础再看) Python Cookbook   一本 Python 3 书籍,非常多的代码示例,适合进一步提升看,有能力的同学还可以看英文版的. Python Web 开发实战 (董伟明 著)Python Web 开发 豆瓣 Python 开发者写的书籍,内

从底层简析Python程序的执行过程

摘要:是否想在Python解释器的内部晃悠一圈?是不是想实现一个Python代码执行的追踪器?没有基础?不要怕,这篇文章让你初窥Python底层的奥妙. [编者按]下面博文将带你创建一个字节码级别的追踪API以追踪Python的一些内部机制,比如类似 YIELDVALUE.YIELDFROM操作码的实现,推式构造列表(List Comprehensions).生成器表达式(generator expressions)以及其他一些有趣Python的编译. 关于译者:赵斌, OneAPM工程师,常年

从底层理解Python的执行

摘要:是否想在Python解释器的内部晃悠一圈?是不是想实现一个Python代码执行的追踪器?没有基础?不要怕,这篇文章让你初窥Python底层的奥妙. [编者按]下面博文将带你创建一个字节码级别的追踪API以追踪Python的一些内部机制,比如类似YIELDVALUE.YIELDFROM操作码的实现,推式构造列表(List Comprehensions).生成器表达式(generator expressions)以及其他一些有趣Python的编译. 关于译者:赵斌, OneAPM工程师,常年使