網站首頁 編程語言 正文
簡介
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語句是要建立運行的上下文,負責執行with語句塊上下文中的進入與退出操作。
- 上下文管理協議:包含方法__enter__()和__exit__(),支持協議的對象要實現這兩個方法
理解完上面的兩點,我們就很容易知道with語句的執行流程,我們看著上面的執行過程代碼來看
- 執行context_expression ,生成山下文管理器context_manager
- 調用上下文管理器的__enter__方法,這個時候如果使用了as,那么就會將__enter__方法的返回值賦值給target
- 執行with-body
- 不管執行過程中是否發生了異常都會執行上下文管理器的__exit__方法,它用于負責執行清理工作,如釋放資源等。
- 如果執行過程中沒有出現異常,或者語句中執行了語句break/continue/return,則以None作為參數調用__exit__(None,None,None);如果執行過程中出現了異常,則使用sys.excinfo得到異常信息作為參數調用__exit__(exc_type,exc_value,exc_traceback)
- 出現異常時,如果__exit__(exc_type,exc_value,exc_traceback)返回false,則會重新拋出異常,讓with完的語句來處理異常,如果__exit__(exc_type,exc_value,exc_traceback)返回True,則異常忽略,不在對于異常來進行處理
代碼
沒有報錯
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__
我們會發現他沒有了報錯信息
原文鏈接:https://blog.csdn.net/youhebuke225/article/details/129098821
- 上一篇:沒有了
- 下一篇:沒有了
相關推薦
- 2022-04-03 Python?webargs?模塊的簡單使用_python
- 2023-03-22 WinPC搭建nginx服務器的實現步驟_nginx
- 2022-04-11 C#基于SerialPort類實現串口通訊詳解_C#教程
- 2022-06-06 在使用element-ui中的el-table組件時,怎么自定義合并表頭和合并行
- 2023-12-15 elementui前端flex布局兼容IE10瀏覽器常用屬性使用
- 2023-01-18 解讀Opencv中Filter2D函數的補全方式_python
- 2023-02-07 C++11中的引用限定符示例代碼_C 語言
- 2022-11-19 C語言數據結構不掛科指南之隊列詳解_C 語言
- 欄目分類
-
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支