with的基本表达式如下
with context_expression [as target(s)]: ... with-body
其中context_expression 可以是任意的表达式,as target(s)是可选的
他的执行过程如下
context_manager = context_expression exit = type(context_manager).__exit__ value = type(context_manager).__enter__(context_manager) exc = True # True 表示正常执行,即便有异常也忽略;False 表示重新抛出异常,需要对异常进行处理 try: try: target = value # 如果使用了 as 子句 with-body # 执行 with-body except: # 执行过程中有异常发生 exc = False # 如果 __exit__ 返回 True,则异常被忽略;如果返回 False,则重新抛出异常 # 由外层代码对异常进行处理 if not exit(context_manager, *sys.exc_info()): raise finally: # 正常退出,或者通过 statement-body 中的 break/continue/return 语句退出 # 或者忽略异常退出 if exc: exit(context_manager, None, None, None) # 缺省返回 None,None 在布尔上下文中看做是 False
首先我们来了解两个概念,上下文管理对象与上下文协议
理解完上面的两点,我们就很容易知道with语句的执行流程,我们看着上面的执行过程代码来看
没有报错
class Test: def __enter__(self): print("__enter__") return "hello python" def __exit__(self, type, value, trace): print("__exit__") pass def get_test(): return Test() with get_test() as test: print(test)
他的打印如下
__enter__
hello python
__exit__
有报错
class Test: def __enter__(self): print("__enter__") return "hello python" def __exit__(self, type, value, trace): print("type", type) print("value", value) print("trace", trace) print("__exit__") def get_test(): return Test() with get_test() as test: print(test) 1 / 0
打印如下
hello python
type <class 'ZeroDivisionError'>
value division by zero
trace <traceback object at 0x0000024938B07B40>
__exit__
Traceback (most recent call last):
File "f:/my_profile/study/code_pub/study_code_pub/python/关键字/with.py", line 35, in <module>
1 / 0
可以看出来当有报错的时候,他是抛出了异常,因为__exit__没有返回值,当他返回True的时候,他的打印如下
当__exit__返回True时
__enter__
hello python
type <class 'ZeroDivisionError'>
value division by zero
trace <traceback object at 0x000001FB117885C0>
__exit__
我们会发现他没有了报错信息