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 相关文章推荐
跟老齐学Python之关于类的初步认识
Oct 11 Python
Python中使用PyQt把网页转换成PDF操作代码实例
Apr 23 Python
Python中isnumeric()方法的使用简介
May 19 Python
python中sys.argv参数用法实例分析
May 20 Python
python实现自动重启本程序的方法
Jul 09 Python
Python入门教程之运算符与控制流
Aug 17 Python
Python使用matplotlib填充图形指定区域代码示例
Jan 16 Python
python多线程调用exit无法退出的解决方法
Feb 18 Python
钉钉群自定义机器人消息Python封装的实例
Feb 20 Python
如何将 awk 脚本移植到 Python
Dec 09 Python
python中pathlib模块的基本用法与总结
Aug 17 Python
selenium+python自动化78-autoit参数化与批量上传功能的实现
Mar 04 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
一个从别的网站抓取信息的例子(域名查询)
2006/10/09 PHP
PHP文件上传之多文件上传的实现思路
2016/01/27 PHP
PHP读书笔记_运算符详解
2016/07/01 PHP
cakephp常见知识点汇总
2017/02/24 PHP
微信公众平台开发-微信服务器IP接口实例(含源码)
2017/03/05 PHP
PHP实现函数内修改外部变量值的方法示例
2018/12/28 PHP
js将字符串转成正则表达式的实现方法
2013/11/13 Javascript
使用jQuery实现验证上传图片的格式与大小
2014/12/03 Javascript
Jquery Ajax xmlhttp请求成功问题
2015/02/04 Javascript
jQuery实现新消息闪烁标题提示的方法
2015/03/11 Javascript
JavaScript常用基础知识强化学习
2015/12/09 Javascript
jQuery弹层插件jquery.fancybox.js用法实例
2016/01/22 Javascript
jQuery增加、删除及修改select option的方法
2016/08/19 Javascript
AngularJS使用ng-app自动加载bootstrap框架问题分析
2017/01/04 Javascript
js实现年月日表单三级联动
2020/04/17 Javascript
浅谈angularjs中响应回车事件
2017/04/24 Javascript
微信小程序实现的3d轮播图效果示例【基于swiper组件】
2018/12/11 Javascript
微信小程序登录session的使用
2019/03/17 Javascript
基于Element封装一个表格组件tableList的使用方法
2020/06/29 Javascript
js实现简单的点名器随机色实例代码
2020/09/20 Javascript
python访问纯真IP数据库的代码
2011/05/19 Python
Python判断操作系统类型代码分享
2014/11/22 Python
在Python中使用SQLite的简单教程
2015/04/29 Python
利用Python获取操作系统信息实例
2016/09/02 Python
python numpy 一维数组转变为多维数组的实例
2018/07/02 Python
Python读取系统文件夹内所有文件并统计数量的方法
2018/10/23 Python
使用Matplotlib 绘制精美的数学图形例子
2019/12/13 Python
torch 中各种图像格式转换的实现方法
2019/12/26 Python
Python tornado上传文件的功能
2020/03/26 Python
python批量提取图片信息并保存的实现
2021/02/05 Python
HTML5实现视频弹幕功能
2019/08/09 HTML / CSS
Europcar德国:全球汽车租赁领域的领导者
2018/08/15 全球购物
个人工作表现自我评价
2015/03/06 职场文书
应急管理工作总结2015
2015/05/04 职场文书
教师网络培训心得体会
2016/01/09 职场文书
linux中nohup和后台运行进程查看及终止
2021/06/24 Python