python中关键词with的使用
来源:互联网 发布:爱宕号重巡洋舰数据 编辑:程序博客网 时间:2024/05/22 03:17
http://stackoverflow.com/questions/3012488/what-is-the-python-with-statement-designed-for
非常好的回答,值得花时间粘过来,粗浅理解就是with利用context managers来为我们要执行的一些操作做预处理和最后的收尾,也就是把try和finally的操作放在了里面,我们再用with调用的时候,就简化了代码。
如最常用的例子
with open('test.txt', 'r') as f:
print f.read()
到这里,f已经close掉了,是因为open自身就是个context manager
后面这个回答者写的几个例子也非常简单明了。也让我第一次感受到装饰器的用处。
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, theopen
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 thewith
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. Thewith
statement can thus be used in ways similar to the RAII pattern in C++: some resource is acquired by thewith
statement and released when you leave thewith
context.Some examples are: opening files using
with open(filename) as fp:
, acquiring locks usingwith lock:
(wherelock
is an instance ofthreading.Lock
). You can also construct your own context managers using thecontextmanager
decorator fromcontextlib
. 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 contextmanagerimport os@contextmanagerdef 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
andsys.stderr
to some other file handle and restores them later:from contextlib import contextmanagerimport sys@contextmanagerdef 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 stdoutprint "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 mkdtempfrom shutil import rmtree@contextmanagerdef 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的使用
- python 中with的使用
- Web Scraping with Python: 使用 Python 爬 Baidu 关键词
- python中with语句的使用
- java 中this关键词的使用
- C++中关键词const的使用
- python中with的用法
- python 中with的用法
- python中with的意义
- Python中With的用法
- Python中with的运行机制
- python中with的用法
- Python中with的用法
- python中with的用法
- python:删除文件中包含关键词的行
- Python:with语句的使用
- params 关键词的使用
- static 关键词的使用
- SDRAM的工作原理
- java学习线路
- xml转换dataset
- git详解
- ios使用rsa加解密
- python中关键词with的使用
- 水果的横向布局
- caffe 所具有的层结构,都有那些层1
- 把一个List拆分为几个大小一样的List
- IO - 同步,异步,阻塞,非阻塞 (亡羊补牢篇)
- Ubuntu 学习问题及解决方法 汇总 持续更新。。。
- linux 时间函数解释
- 浅谈CPSR程序状态寄存器
- android studio