python之with语句的原理
首发时间:2018-02-23 15:28
之前看到一篇博客说博主python面试时遇到面试官提问with的原理,而那位博主的博文没有提及with原理,故有此文。
关于with语句,官方文档中是这样描述的:
The with statement is used to wrap the execution of a block with methods defined by a context manager (see section With Statement Context Managers). This allows common try...except...finally usage patterns to be encapsulated for convenient reuse.
with_stmt ::= "with" with_item ("," with_item)* ":"
suitewith_item ::=
expression["as"target]
The execution of the with statement with one “item” proceeds as follows:
The context expression (the expression given in the with_item) is evaluated to obtain a context manager.
The context manager’s __exit__() is loaded for later use.
The context manager’s __enter__() method is invoked.
If a target was included in the with statement, the return value from __enter__() is assigned to it.
Note
The with statement guarantees that if the __enter__() method returns without an error, then __exit__() will always be called. Thus, if an error occurs during the assignment to the target list, it will be treated the same as an error occurring within the suite would be. See step 6 below.
The suite is executed.
The context manager’s __exit__() method is invoked. If an exception caused the suite to be exited, its type, value, and traceback are passed as arguments to __exit__(). Otherwise, three None arguments are supplied.
谷歌翻译成中文就是:
with语句用于使用由上下文管理器定义的方法来封装块的执行(请参见使用语句上下文管理器一节)。 这允许通用的try…except…finally使用模式被封装以便于重用【这句话大概意思就是“with语句”类似于try…except…finally封装之后的的情况】。
带有一个“项目”的with语句的执行过程如下:
1.上下文表达式(在with_item中给出的表达式)被评估以获得上下文管理器。【会区分类型来处理,如文件,进程等都可以使用with语句】
2.上下文管理器的__exit __()被加载供以后使用。【负责上下文的退出】
3.上下文管理器的__enter __()方法被调用。【负责上下文的进入】
4.如果在with语句中包含目标,则将__enter __()的返回值分配给它。【如果with后面跟着as 对象(如with open() as f),那么此对象获得with上下文对象的__enter__()的返回值,(附:应该是类似操作数据库时的连接对象和游标的区别)】
注意
with语句保证,如果__enter __()方法返回时没有错误,那么将始终调用__exit __()。 因此,如果在分配给目标列表期间发生错误,它将被视为与套件内发生的错误相同。 请参阅下面的第6步。
5.该套件已执行。【意思就是语句体中的过程执行完毕,执行完毕就到第六步--调用__exit__()来退出】
6.上下文管理器的__exit __()方法被调用。 如果异常导致套件退出,则其类型,值和回溯作为参数传递给__exit __()。 否则,将提供三个无参数。
关于退出返回值: If the suite was exited due to an exception, and the return value from the __exit__() method was false, the exception is reraised. If the return value was true, the exception is suppressed, and execution continues with the statement following the with statement. If the suite was exited for any reason other than an exception, the return value from __exit__() is ignored, and execution proceeds at the normal location for the kind of exit that was taken. 中文:
如果套件由于异常而退出,并且__exit __()方法的返回值为false,则会重新对异常进行重新评估。 如果返回值为true,则异常被抑制,并继续执行with语句后面的语句。 如果套件由于除了异常之外的任何原因而退出,则__exit __()的返回值将被忽略,并且执行将在正常位置继续进行。
意思就是:
如果是异常退出,那么会返回false,(根据文档中的exit的描述“that __exit__() methods should not reraise the passed-in exception; this is the caller’s responsibility.”,大概意思就是exit()不会处理异常,会重新抛出异常抛出给外面,由调用者处理,因为这是调用者的责任)
如果返回 True,则忽略异常,不再对异常进行处理【(在exit内部处理完异常后,可以让”__exit__()”方法返回True,此时该异常就会不会再被抛出,with会认为它的执行体没有发生异常)】
(with会识别返回值,根据返回值来处理,如果是False,那么with会将执行体中的异常抛出,如果是True,那么with会认为没有发生异常(忽略异常),而继续执行外面的语句,但由于内部调用的了__exit__(),所以在异常之后的语句是不会运行的)
附上一个文档中提供的一个关于with中使用锁的例子:

几个测试:
1.执行体中发生异常:
import time
class myContextDemo(object):
def __init__(self,gen):
self.gen = gen
def __enter__(self):
print("enter in ")
return self.gen
def __exit__(self, exc_type, exc_val, exc_tb):
#exc_type是exception_type exc_val是exception_value exc_tb是exception_trackback
print("exit in ")
if exc_type is None:#如果是None 则继续执行
print("None:",exc_type, exc_val, exc_tb) else: #异常不为空时执行,这一步,如果with语句体中发生异常,那么也会执行
print("exception:", exc_type, exc_val, exc_tb)
print("all done") if __name__=="__main__":
gen=(i for i in range(5,10))
G=myContextDemo(gen)
with G as f :
print("hello")
for i in f:
print(i,end="\t")
#测试1:执行体中发生异常
raise Exception("母鸡啊")
print("main continue")
结果显示:
1.抛出异常后,后面main continue不再执行
2.__exit__()中的else会执行
测试2:当else中强制返回为True时:
import time
class myContextDemo(object):
def __init__(self,gen):
self.gen = gen
def __enter__(self):
print("enter in ")
return self.gen
def __exit__(self, exc_type, exc_val, exc_tb):
#exc_type是exception_type exc_val是exception_value exc_tb是exception_trackback
print("exit in ")
if exc_type is None:#如果是None 则继续执行
print("None:",exc_type, exc_val, exc_tb) else: #异常不为空时执行,这一步,如果with语句体中发生异常,那么也会执行
print("exception:", exc_type, exc_val, exc_tb)
print("all done")
return True #这里如果返回true可以看到发生异常后,main continue可以执行
#即,如果exc_type是true,那么会继续执行,实际上,也可以在这里处理一下异常再返回true if __name__=="__main__":
gen=(i for i in range(5,10))
G=myContextDemo(gen)
with G as f :
print("hello")
for i in f:
print(i,end="\t")
raise Exception("母鸡啊")
# print("continue")#这里不会执行
print("main continue")
结果显示:
1.返回True之后,with会忽略异常,继续执行,所以这里“main continue”能执行
2.即使忽略异常,在with体中异常之后的语句依旧不会执行
附:理论上可以在返回True之前处理一下异常
PS:如果大家想要了解得更详细,可以自己尝试去读一下官方文档。
附上关于with语句的详细介绍官方文档:https://www.python.org/dev/peps/pep-0343/
python之with语句的原理的更多相关文章
- 理解python的with语句
Python’s with statement provides a very convenient way of dealing with the situation where you have ...
- Python之with语句
Python之with语句 在Python中,我们在打开文件的时候,为了代码的健壮性,通常要考虑一些异常情况,比如: try: ccfile = open('/path/data') content ...
- (Python )控制流语句if、for、while
这一节,我们将学习Python的控制流语句,主要包括if.for.while.break.continue 和pass语句 1. If语句 if语句也许是我们最熟悉的语句.其使用方法如下: x=inp ...
- Python的with语句
写过多线程程序的人肯定对各种锁很熟悉,尤其是下面这种代码 def lock_usage: lock.Lock() if(...) : lock.Unlock() return lock.Unlock( ...
- Python学习教程(learning Python)--3.3.4 Python的if-elif-else语句
Python的if-elif-else语句用于多种条件判断后选择某个语句块执行.该语句可以利用一系列条件表达式进行检查,并在某个表达式为真的情况下执行相应的代码.需要注意的是,虽然if/elif/el ...
- 转: 理解Python的With语句
Python’s with statement provides a very convenient way of dealing with the situation where you have ...
- python的with语句,超级强大
With语句是什么? 有一些任务,可能事先需要设置,事后做清理工作.对于这种场景,Python的with语句提供了一种非常方便的处理方式.一个很好的例子是文件处理,你需要获取一个文件句柄,从文件中读取 ...
- 简单探讨python中的语句和语法
python程序结构 python"一切皆对象",这是接触python听到最多的总结了.在python中最基层的单位应该就是对象了,对象需要靠表达式建立处理,而表达式往往存在于语句 ...
- python基础——继承实现的原理
python基础--继承实现的原理 1 继承顺序 class A(object): def test(self): print('from A') class B(A): def test(self) ...
随机推荐
- windows上xshell6的安装
各位大兄弟,好用的xshell组件来了,话不多说,开搞. 软件链接链接:https://pan.baidu.com/s/1vcRo2L-LNe2BrJ9-VCy57A 密码:ei73 有下面四个软件, ...
- python中实现排序list
作为一个非常实用的一种数据结构,排序链表用在很多方面,下面是它的python代码实现: from Node import * class OrderedList: def __init__(self) ...
- Linux - YUM包管理
简述 rpm是由红帽公司开发的软件包管理方式,使用rpm可以方便的进行软件的安装.查询.卸载.升级等工作. 但是rpm软件包之间的依赖性问题往往会很繁琐,尤其是软件由多个rpm包组成时. Yum(全称 ...
- C# DataGridView 在最左侧显示行号方法
代码: private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e) { Da ...
- 【WebAPI No.5】Core WebAPI中的自定义格式化
介绍 Web API为JSON和XML提供媒体类型格式化程序.框架默认将这些格式化程序插入管道中.客户端可以在HTTP请求的Accept标头中请求JSON或XML. 格式化数据这个东西,其实没有什么最 ...
- IMEI
IMEI(International Mobile Equipment Identity)是国际移动设备身份码的缩写,国际移动装备辨识码,是由15位数字组成的"电子串号",它与每台 ...
- 从零开始学 Web 之 jQuery(二)获取和操作元素的属性
大家好,这里是「 从零开始学 Web 系列教程 」,并在下列地址同步更新...... github:https://github.com/Daotin/Web 微信公众号:Web前端之巅 博客园:ht ...
- Spring Boot + Spring Cloud 实现权限管理系统 后端篇(十八):注册中心(Spring Cloud Consul)
什么是 Consul Consul 是 HashiCorp 公司推出的开源工具,用于实现分布式系统的服务发现与配置.与其它分布式服务注册与发现的方案,Consul 的方案更“一站式”,内置了服务注册与 ...
- 自己实现的ajax分页js
/** * 分页 * @author chenqionghe * @param data json数据,包含totalPage,count * @param page 获取的页数 * @param o ...
- 完整的一次 HTTP 请求响应过程(二)
上篇文章 我们完整的描述了计算机五层模型中的『应用层』和『运输层』,阐述了较为复杂的 TCP 协议的相关原理,相信大家一定也有所收获,那么本篇将继续五层模型的学习. 网络层 『网络层』其实解决的就是一 ...