Python with用法实例


Posted in Python onApril 14, 2015

python中with可以明显改进代码友好度,比如:

with open('a.txt') as f:  

    print f.readlines() 

为了我们自己的类也可以使用with, 只要给这个类增加两个函数__enter__, __exit__即可:
>>> class A:  

    def __enter__(self):  

        print 'in enter'  

    def __exit__(self, e_t, e_v, t_b):  

        print 'in exit'  

  

>>> with A() as a:  

    print 'in with'  

  

in enter  

in with  

in exit 

另外python库中还有一个模块contextlib,使你不用构造含有__enter__, __exit__的类就可以使用with:
>>> from contextlib import contextmanager  

>>> from __future__ import with_statement  

>>> @contextmanager  

... def context():  

...     print 'entering the zone'  

...     try:  

...         yield  

...     except Exception, e:  

...         print 'with an error %s'%e  

...         raise e  

...     else:  

...         print 'with no error'  

...  

>>> with context():  

...     print '----in context call------'  

...  

entering the zone  

----in context call------  

with no error 

使用的最多的就是这个contextmanager, 另外还有一个closing 用处不大
from contextlib import closing  

import urllib  

  

with closing(urllib.urlopen('http://www.python.org')) as page:  

    for line in page:  

        print line 
Python 相关文章推荐
linux系统使用python监测系统负载脚本分享
Jan 15 Python
Python不规范的日期字符串处理类
Jun 10 Python
python实现监控linux性能及进程消耗性能的方法
Jul 25 Python
Python单元测试框架unittest使用方法讲解
Apr 13 Python
常见的python正则用法实例讲解
Jun 21 Python
python中利用Future对象回调别的函数示例代码
Sep 07 Python
python版学生管理系统
Jan 10 Python
python命令行解析之parse_known_args()函数和parse_args()使用区别介绍
Jan 24 Python
在pandas多重索引multiIndex中选定指定索引的行方法
Nov 16 Python
Python和Sublime整合过程图示
Dec 25 Python
8种常用的Python工具
Aug 05 Python
Python+OpenCV实现图片中的圆形检测
Apr 07 Python
详细探究Python中的字典容器
Apr 14 #Python
Python中decorator使用实例
Apr 14 #Python
用Python创建声明性迷你语言的教程
Apr 13 #Python
Python中的Numeric包和Numarray包使用教程
Apr 13 #Python
Python中一些自然语言工具的使用的入门教程
Apr 13 #Python
用Python的SimPy库简化复杂的编程模型的介绍
Apr 13 #Python
Python中用Decorator来简化元编程的教程
Apr 13 #Python
You might like
php与java通过socket通信的实现代码
2013/10/21 PHP
destoon二次开发模板及调用语法汇总
2014/06/21 PHP
php超快高效率统计大文件行数
2015/07/05 PHP
ext form 表单提交数据的方法小结
2008/08/08 Javascript
jQuery去掉字符串起始和结尾的空格(多种方法实现)
2013/04/01 Javascript
Javascript学习笔记之 对象篇(四) : for in 循环
2014/06/24 Javascript
jQuery超赞的评分插件(8款)
2015/08/20 Javascript
js淡入淡出的图片轮播效果代码分享
2015/08/24 Javascript
js实现显示当前状态的导航效果代码
2015/08/28 Javascript
jQuery新窗口打开外链接
2016/07/21 Javascript
JavaScript实现窗口抖动效果
2016/10/19 Javascript
javascript设置文本框光标的方法实例小结
2016/11/04 Javascript
validationEngine 表单验证插件使用实例代码
2017/06/15 Javascript
JS中把函数作为另一函数的参数传递方法(总结)
2017/06/28 Javascript
基于JS对象创建常用方式及原理分析
2017/06/28 Javascript
AngularJS中controller控制器继承的使用方法
2017/11/03 Javascript
jQuery实现鼠标滑过商品小图片上显示对应大图片功能【测试可用】
2018/04/27 jQuery
python xml.etree.ElementTree遍历xml所有节点实例详解
2016/12/04 Python
pandas求两个表格不相交的集合方法
2018/12/08 Python
python+opencv实现阈值分割
2018/12/26 Python
Django Rest framework解析器和渲染器详解
2019/07/25 Python
django和vue实现数据交互的方法
2019/08/21 Python
在jupyter notebook中调用.ipynb文件方式
2020/04/14 Python
Pycharm中配置远程Docker运行环境的教程图解
2020/06/11 Python
聊聊python中的异常嵌套
2020/09/01 Python
jupyter使用自动补全和切换默认浏览器的方法
2020/11/18 Python
BeautifulSoup中find和find_all的使用详解
2020/12/07 Python
HTML5中判断横屏竖屏的方法(移动端)
2016/08/04 HTML / CSS
德国拖鞋网站:German Slippers
2019/11/08 全球购物
俄罗斯第一家篮球店:StreetBall
2020/07/30 全球购物
体育活动总结
2015/02/04 职场文书
晚会开场白和结束语
2015/05/29 职场文书
入党积极分子半年考察意见
2015/06/02 职场文书
有关朝花夕拾的读书笔记
2015/06/29 职场文书
苹果的回收机器人可以通过拆解iPhone获取大量的金和铜并外公布了环境保护最新进展
2022/04/21 数码科技
PYTHON基于Pyecharts绘制常见的直角坐标系图表
2022/04/28 Python