我今天第一次遇到Python with语句。我已经简单地使用Python几个月了,甚至不知道它的存在!鉴于它的地位有些模糊,我认为有必要问一下:
Python with语句是什么 设计用于? 是什么 你用它干什么? 有吗? 我需要注意的问题,还是 相关联的常见反模式 它的使用?在什么情况下try. finally比with更好? 为什么它没有被更广泛地使用呢? 哪些标准库类与它兼容?
我今天第一次遇到Python with语句。我已经简单地使用Python几个月了,甚至不知道它的存在!鉴于它的地位有些模糊,我认为有必要问一下:
Python with语句是什么 设计用于? 是什么 你用它干什么? 有吗? 我需要注意的问题,还是 相关联的常见反模式 它的使用?在什么情况下try. finally比with更好? 为什么它没有被更广泛地使用呢? 哪些标准库类与它兼容?
当前回答
with语句适用于所谓的上下文管理器:
http://docs.python.org/release/2.5.2/lib/typecontextmanager.html
这个想法是通过在离开with块后进行必要的清理来简化异常处理。一些python内置程序已经作为上下文管理器工作。
其他回答
I believe this has already been answered by other users before me, so I only add it for the sake of completeness: the with statement simplifies exception handling by encapsulating common preparation and cleanup tasks in so-called context managers. More details can be found in PEP 343. For instance, the open statement is a context manager in itself, which lets you open a file, keep it open as long as the execution is in the context of the with statement where you used it, and close it as soon as you leave the context, no matter whether you have left it because of an exception or during regular control flow. The with statement can thus be used in ways similar to the RAII pattern in C++: some resource is acquired by the with statement and released when you leave the with context. Some examples are: opening files using with open(filename) as fp:, acquiring locks using with lock: (where lock is an instance of threading.Lock). You can also construct your own context managers using the contextmanager decorator from contextlib. For instance, I often use this when I have to change the current directory temporarily and then return to where I was: from contextlib import contextmanager import os @contextmanager def working_directory(path): current_dir = os.getcwd() os.chdir(path) try: yield finally: os.chdir(current_dir) with working_directory("data/stuff"): # do something within data/stuff # here I am back again in the original working directory Here's another example that temporarily redirects sys.stdin, sys.stdout and sys.stderr to some other file handle and restores them later: from contextlib import contextmanager import sys @contextmanager def redirected(**kwds): stream_names = ["stdin", "stdout", "stderr"] old_streams = {} try: for sname in stream_names: stream = kwds.get(sname, None) if stream is not None and stream != getattr(sys, sname): old_streams[sname] = getattr(sys, sname) setattr(sys, sname, stream) yield finally: for sname, stream in old_streams.iteritems(): setattr(sys, sname, stream) with redirected(stdout=open("/tmp/log.txt", "w")): # these print statements will go to /tmp/log.txt print "Test entry 1" print "Test entry 2" # back to the normal stdout print "Back to normal stdout again" And finally, another example that creates a temporary folder and cleans it up when leaving the context: from tempfile import mkdtemp from shutil import rmtree @contextmanager def temporary_dir(*args, **kwds): name = mkdtemp(*args, **kwds) try: yield name finally: shutil.rmtree(name) with temporary_dir() as dirname: # do whatever you want
Python with语句是c++中常用的“资源获取即初始化”习语的内置语言支持。它的目的是允许安全获取和释放操作系统资源。
with语句在作用域/块内创建资源。您可以使用块中的资源编写代码。当块退出时,无论块中代码的结果如何(即块是正常退出还是因为异常退出),资源都将被干净地释放。
Python库中的许多资源遵循with语句所要求的协议,因此可以与它一起开箱即用。然而,任何人都可以通过实现记录良好的协议PEP 0343来创建可以在with语句中使用的资源
当您在应用程序中获取必须显式放弃的资源(如文件、网络连接、锁等)时,请使用它。
在python中,通常使用" with "语句打开文件,处理文件中的数据,并在不调用close()方法的情况下关闭文件。" with "语句通过提供清理活动简化了异常处理。
with的一般形式:
with open(“file name”, “mode”) as file_var:
processing statements
注意:不需要在file_var.close()上调用close()来关闭文件
同样,为了完整起见,我将添加最有用的with语句用例。
我做了很多科学计算,对于一些活动,我需要Decimal库进行任意精度计算。我的代码的某些部分我需要较高的精度,而对于大多数其他部分我需要较低的精度。
我将我的默认精度设置为一个较低的数字,然后使用with来获得一些部分的更精确的答案:
from decimal import localcontext
with localcontext() as ctx:
ctx.prec = 42 # Perform a high precision calculation
s = calculate_something()
s = +s # Round the final result back to the default precision
我在Hypergeometric Test中经常使用这种方法,因为它需要对大数进行阶乘。当你进行基因组规模计算时,你必须小心四舍五入和溢出错误。
另一个开箱即用支持的例子是流行数据库模块的连接对象,例如:
sqlite3 psycopg2 cx_oracle
连接对象是上下文管理器,因此可以在with-statement中开箱即用,但是在使用上述方法时请注意:
当with-block结束时,无论是否出现异常,连接都不会关闭。如果with-block以异常结束,则事务将回滚,否则事务将被提交。
这意味着程序员必须小心自己关闭连接,但允许获取一个连接,并在多个with-语句中使用它,如psycopg2文档所示:
conn = psycopg2.connect(DSN)
with conn:
with conn.cursor() as curs:
curs.execute(SQL1)
with conn:
with conn.cursor() as curs:
curs.execute(SQL2)
conn.close()
在上面的示例中,您将注意到psycopg2的游标对象也是上下文管理器。从行为的相关文档中:
当游标退出with-block时,游标将被关闭,释放与游标最终关联的所有资源。事务的状态不会受到影响。