python 中 with as的用法
来源:互联网 发布:linux系统挂载光盘 编辑:程序博客网 时间:2024/06/09 04:31
with从Python 2.5就有,需要from __future__ import with_statement。
自python 2.6开始,成为默认关键字。
在What's new in python2.6/3.0中,明确提到:
The ‘
with
‘ statement is a control-flow structure whose basic
structure is:
with expression [as variable]:
with-block
也就是说with是一个控制流语句,跟if/for/while/try之类的是一类的,with可以用来简化try finally代码,看起来可以比try finally更清晰。
这里新引入了一个"上下文管理协议"context management protocol,实现方法是为一个类定义__enter__和__exit__两个函数。
with expresion as variable的执行过程是,首先执行__enter__函数,它的返回值会赋给as后面的variable,想让它返回什么就返回什么,只要你知道怎么处理就可以了,如果不写as variable,返回值会被忽略。
然后,开始执行with-block中的语句,不论成功失败(比如发生异常、错误,设置sys.exit()),在with-block执行完成后,会执行__exit__函数。
这样的过程其实等价于:
try:
执行 __enter__的内容
执行 with_block.
finally:
执行 __exit__内容
只不过,现在把一部分代码封装成了__enter__函数,清理代码封装成__exit__函数。
我们可以自己实现一个例子:
import sys
class test:
def __enter__(self):
print("enter")
return 1
def __exit__(self,*args):
print("exit")
return True
with test() as t:
print("t is not the result of test(), it is __enter__ returned")
print("t is 1, yes, it is {0}".format(t))
raise NameError("Hi there")
sys.exit()
print("Never here")
注意:
1,t不是test()的值,test()返回的是"context manager object",是给with用的。t获得的是__enter__函数的返回值,这是with拿到test()的对象执行之后的结果。t的值是1.
2,__exit__函数的返回值用来指示with-block部分发生的异常是否要re-raise,如果返回False,则会re-raise with-block的异常,如果返回True,则就像什么都没发生。
自python 2.6开始,成为默认关键字。
在What's new in python2.6/3.0中,明确提到:
The ‘
with
‘ statement is a control-flow structure whose basic
structure is:
with expression [as variable]:
with-block
也就是说with是一个控制流语句,跟if/for/while/try之类的是一类的,with可以用来简化try finally代码,看起来可以比try finally更清晰。
这里新引入了一个"上下文管理协议"context management protocol,实现方法是为一个类定义__enter__和__exit__两个函数。
with expresion as variable的执行过程是,首先执行__enter__函数,它的返回值会赋给as后面的variable,想让它返回什么就返回什么,只要你知道怎么处理就可以了,如果不写as variable,返回值会被忽略。
然后,开始执行with-block中的语句,不论成功失败(比如发生异常、错误,设置sys.exit()),在with-block执行完成后,会执行__exit__函数。
这样的过程其实等价于:
try:
执行 __enter__的内容
执行 with_block.
finally:
执行 __exit__内容
只不过,现在把一部分代码封装成了__enter__函数,清理代码封装成__exit__函数。
我们可以自己实现一个例子:
import sys
class test:
def __enter__(self):
print("enter")
return 1
def __exit__(self,*args):
print("exit")
return True
with test() as t:
print("t is not the result of test(), it is __enter__ returned")
print("t is 1, yes, it is {0}".format(t))
raise NameError("Hi there")
sys.exit()
print("Never here")
注意:
1,t不是test()的值,test()返回的是"context manager object",是给with用的。t获得的是__enter__函数的返回值,这是with拿到test()的对象执行之后的结果。t的值是1.
2,__exit__函数的返回值用来指示with-block部分发生的异常是否要re-raise,如果返回False,则会re-raise with-block的异常,如果返回True,则就像什么都没发生。
- python 中 with as的用法
- python 中关于with...as的用法
- Python中with...as的用法
- python中 with...as的用法
- python中with as 用法
- python中 with as 用法
- python中with as 用法
- python的with...as...用法
- python with as的用法
- python with as的用法
- Python中关于with open file as 的用法
- python with...as...用法
- python with...as用法
- python with...as...用法
- python with...as...用法
- python with as 用法
- python with as用法
- python with as 用法
- sh细节
- 《数据通信与网络》笔记--有线局域网:以太网
- UVa 10465 HomerSimpson
- 部署Feature时报功能未安装在此服务器场中,无法添加到该范围
- iOS单例的创建方式
- python 中 with as的用法
- sublime Text 2取消自动检查更新
- JS计算前一天或后一天等
- Python3入门学习以及与2x版本的变化部位整理(持续)
- java 23种设计模式
- 黑马程序员----C#中的流程控制语句之异常处理语句
- BZOJ 1049([HAOI2006]数字序列-数字序列LIS与分块)
- hdu 4546 输入n个数 问这n个数相加组成的第K小的数是什么
- platform设备驱动简介