欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python中with的具體用法

 更新時間:2023年02月23日 08:32:21   作者:youhebuke225  
本文主要介紹了python中with的基本使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

簡介

with的基本表達式如下

with context_expression [as target(s)]:
    ...
    with-body

其中context_expression 可以是任意的表達式,as target(s)是可選的

他的執(zhí)行過程如下

context_manager = context_expression
exit = type(context_manager).__exit__
value = type(context_manager).__enter__(context_manager)
exc = True   # True 表示正常執(zhí)行,即便有異常也忽略;False 表示重新拋出異常,需要對異常進行處理
try:
    try:
        target = value  # 如果使用了 as 子句
        with-body     # 執(zhí)行 with-body
    except:
        # 執(zhí)行過程中有異常發(fā)生
        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

深入

首先我們來了解兩個概念,上下文管理對象與上下文協(xié)議

  • 上下文管理器:支持上下文管理協(xié)議的對象,這里我們就要知道什么是上下文管理管理協(xié)議了,請看第二點。上下文管理器運行with語句是要建立運行的上下文,負(fù)責(zé)執(zhí)行with語句塊上下文中的進入與退出操作。
  • 上下文管理協(xié)議:包含方法__enter__()和__exit__(),支持協(xié)議的對象要實現(xiàn)這兩個方法

理解完上面的兩點,我們就很容易知道with語句的執(zhí)行流程,我們看著上面的執(zhí)行過程代碼來看

  • 執(zhí)行context_expression ,生成山下文管理器context_manager
  • 調(diào)用上下文管理器的__enter__方法,這個時候如果使用了as,那么就會將__enter__方法的返回值賦值給target
  • 執(zhí)行with-body
  • 不管執(zhí)行過程中是否發(fā)生了異常都會執(zhí)行上下文管理器的__exit__方法,它用于負(fù)責(zé)執(zhí)行清理工作,如釋放資源等。
  • 如果執(zhí)行過程中沒有出現(xiàn)異常,或者語句中執(zhí)行了語句break/continue/return,則以None作為參數(shù)調(diào)用__exit__(None,None,None);如果執(zhí)行過程中出現(xiàn)了異常,則使用sys.excinfo得到異常信息作為參數(shù)調(diào)用__exit__(exc_type,exc_value,exc_traceback)
  • 出現(xiàn)異常時,如果__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/關(guān)鍵字/with.py", line 35, in <module>
    1 / 0

可以看出來當(dāng)有報錯的時候,他是拋出了異常,因為__exit__沒有返回值,當(dāng)他返回True的時候,他的打印如下

當(dāng)__exit__返回True時

__enter__
hello python
type <class 'ZeroDivisionError'>
value division by zero
trace <traceback object at 0x000001FB117885C0>
__exit__

我們會發(fā)現(xiàn)他沒有了報錯信息

到此這篇關(guān)于python中with的具體用法的文章就介紹到這了,更多相關(guān)python with使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論