Python 上下文管理 魔术方法
1、上下文管理对象
当一个对象同时实现了__enter__()
和__exit__()
方法,它就属于上下文管理的对象。
-
__enter__
:进入与此对象相关的上下文。如果存在该方法,with语法会把该方法的返回值作为绑定到as子句中指定的变量上 -
__exit__
:退出与此对象相关的上下文 -
参数介绍
__enter__方法,没有其它参数 __exit__方法有3个参数: __exit__(self, exc_type, exc_val, exc_tb) 这三个参数都与异常有关 如果该上下文退出时没有异常,这三个参数都是None 如果有异常,参数意义如下 exc_type:异常类型 exc_val:异常的值 exc_tb:异常的追踪信息 __exit__方法返回一个等效True的值,则压制异常;否则,继续抛出异常
1.1 注意事项
- 实例化对象的时候,并不会调用
__enter__
- 进入
with
语句块调用__enter__
方法,然后执行语句体 - 最后离开
with
语句块的时候,调用__exit__
方法 with
可以开启一个上下文运行环境,在执行前做一些准备工作,执行后做一些收尾工作- 注意,
with
并不开启一个新的作用域
1.2 示例
import time
class Point:
def __init__(self):
print('init ======')
time.sleep(1)
print('init over')
def __enter__(self):
print('enter ------')
def __exit__(self, exc_type, exc_val, exc_tb):
print('exit ++++++')
with Point() as p:
print('in with ---------')
time.sleep(2)
print('with over')
print('======end======')
init ======
init over
enter ------
in with ---------
with over
exit ++++++
======end======
2、异常对上下文的影响
- 通过此例可以看出在有异常抛出的时候(即使抛出
sys.exit()
异常) enter
和exit
照样执行,上下文管理时安全的with
语法,会调用with
后的对象的__enter__
方法with
语法后如果有as
,则将该方法的返回值赋给as
子句的变量
import time
class Point:
def __init__(self):
print('init ======')
time.sleep(1)
print('init over')
def __enter__(self):
print('enter ------')
def __exit__(self, exc_type, exc_val, exc_tb):
print('exit ++++++')
with Point() as p:
print('in with ---------')
raise Exception('error')
time.sleep(2)
print('with over')
print('======end======')
init ======
init over
enter ------
in with ---------
exit ++++++
---------------------------------------------------------------------------
Exception Traceback (most recent call last)
16 print('in with ---------')
-