我今天第一次遇到Python with语句。我已经简单地使用Python几个月了,甚至不知道它的存在!鉴于它的地位有些模糊,我认为有必要问一下:

Python with语句是什么 设计用于? 是什么 你用它干什么? 有吗? 我需要注意的问题,还是 相关联的常见反模式 它的使用?在什么情况下try. finally比with更好? 为什么它没有被更广泛地使用呢? 哪些标准库类与它兼容?


当前回答

反模式的一个例子可能是在循环内部使用with,而在循环外部使用with会更有效

例如

for row in lines:
    with open("outfile","a") as f:
        f.write(row)

vs

with open("outfile","a") as f:
    for row in lines:
        f.write(row)

第一种方法是为每一行打开和关闭文件,与第二种只打开和关闭一次文件的方法相比,这可能会导致性能问题。

其他回答

参见PEP 343 - 'with'语句,末尾有一个示例部分。

... 新的语句"with"到Python 制造语言 可以排除try/finally语句的标准用法。

with语句适用于所谓的上下文管理器:

http://docs.python.org/release/2.5.2/lib/typecontextmanager.html

这个想法是通过在离开with块后进行必要的清理来简化异常处理。一些python内置程序已经作为上下文管理器工作。

我想推荐两堂有趣的课:

PEP 343 with语句 Effbot理解Python的 ”与“声明

1. with语句用于用上下文管理器定义的方法包装块的执行。这允许普通的尝试…最后对使用模式进行封装,以方便重用。

2. 你可以这样做:

with open("foo.txt") as foo_file:
    data = foo_file.read()

OR

from contextlib import nested
with nested(A(), B(), C()) as (X, Y, Z):
   do_something()

OR (Python 3.1)

with open('data') as input_file, open('result', 'w') as output_file:
   for line in input_file:
     output_file.write(parse(line))

OR

lock = threading.Lock()
with lock:
    # Critical section of code

3. 我在这里没有看到任何反模式。 深入了解Python:

试一试,终于好了。有更好。

4. 我想这与程序员使用try..catch..的习惯有关。最后是来自其他语言的语句。

反模式的一个例子可能是在循环内部使用with,而在循环外部使用with会更有效

例如

for row in lines:
    with open("outfile","a") as f:
        f.write(row)

vs

with open("outfile","a") as f:
    for row in lines:
        f.write(row)

第一种方法是为每一行打开和关闭文件,与第二种只打开和关闭一次文件的方法相比,这可能会导致性能问题。

第1点、第2点和第3点得到了很好的阐述:

4:它相对较新,仅在python2.6+中可用(或python2.5使用from __future__ import with_statement)