python with用法
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 with用法的更多相关文章
- Python高级用法总结
Python很棒,它有很多高级用法值得细细思索,学习使用.本文将根据日常使用,总结介绍Python的一组高级特性,包括:列表推导式.迭代器和生成器.装饰器. 列表推导(list comprehensi ...
- python argparse用法总结
转:python argparse用法总结 1. argparse介绍 argparse是python的一个命令行解析包,非常适合用来编写可读性非常好的程序. 2. 基本用法 prog.py是我在li ...
- Anaconda下载及安装及查看安装的Python库用法
Anaconda下载及安装及查看安装的Python库用法 Anaconda 是一个用于科学计算的 Python 发行版,提供了包管理与环境管理的功能.Anaconda 利用 conda 来进行 pac ...
- python enumerate用法总结【转】
enumerate()说明 enumerate()是python的内置函数 enumerate在字典上是枚举.列举的意思 对于一个可迭代的(iterable)/可遍历的对象(如列表.字符串),enum ...
- Python高级用法
Python高级用法 三元表达式 x = 10 y = 20 print(x if x > y else y) x = 100 y = 20 print(x if x > y else y ...
- Python import用法以及与from...import的区别
Python import用法以及与from...import的区别 在python用import或者from...import来导入相应的模块.模块其实就是一些函数和类的集合文件,它能实现一些相应的 ...
- 预备知识-python核心用法常用数据分析库(上)
1.预备知识-python核心用法常用数据分析库(上) 目录 1.预备知识-python核心用法常用数据分析库(上) 概述 实验环境 任务一:环境安装与配置 [实验目标] [实验步骤] 任务二:Pan ...
- python sorted用法
python列表排序 python字典排序 sorted List的元素可以是各种东西,字符串,字典,自己定义的类等. sorted函数用法如下: sorted(data, cmp=None, key ...
- 转 python range 用法
详细记录python的range()函数用法 使用python的人都知道range()函数很方便,今天再用到他的时候发现了很多以前看到过但是忘记的细节.这里记录一下range(),复习下list的 ...
- python yield用法举例说明
1 yield基本用法 典型的例子: 斐波那契(Fibonacci)數列是一个非常简单的递归数列,除第一个和第二个数外,任意一个数都可由前两个数相加得到.1 2 3 5 8…… def fab(ma ...
随机推荐
- ratingbar设置不可调节星星数量
<RatingBar android:id="@+id/rb_bar" android:layout_width="wrap_content" andro ...
- RocketMQ源码 — 三、 Producer消息发送过程
Producer 消息发送 producer start producer启动过程如下图 public void start(final boolean startFactory) throws MQ ...
- POJ 2155 Matrix(二维树状数组)
与以往不同的是,这个树状数组是二维的,仅此而已 #include <iostream> #include <cstdio> #include <cstring> # ...
- DataGridView很详细的用法
DataGridiew用法总结 一.DataGridView 取得或者修改当前单元格的内容: 当前单元格指的是 DataGridView 焦点所在的单元格,它可以通过 DataGridView 对象的 ...
- B. Qualifying Contest
time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...
- word 书签排序算法
直接上代码 /// <summary> /// 通过计算插入引文的位置格式化合适的引文序号 /// </summary> /// <returns></ret ...
- [算法] kmp实现
字符串查找是经典场景,也是面试中最常见的一道题. 说来惭愧,毕业3年了,才明白了kmp算法的实现,以前一直以为这类算法是基础,工作中中不会碰到[也的确没有碰到过...] 但是,对这些基本算法结构的理解 ...
- Hibernate与IBatis的优缺点及可行性分析
以下文章来源于考试大 1.优点 简单: 易于学习,易于使用,通过文档和源代码,可以比较完全的掌握它的设计思路和实现. 实用: 提供了数据映射功能,提供了对底层数据访问的封装(例如ado.net),提供 ...
- iOS navigationBar 的isTranslucent属性
苹果文档: A Boolean value indicating whether the navigation bar is translucent (YES) or not (NO). The de ...
- 用C语言写一个“事件”的模拟程序
源:用C语言写一个“事件”的模拟程序 Example.c //定义一个函数指针 func int (*func) (void); //调用该函数相当于触发了事件. //该事件触发后,会检查函数指针fu ...