久久福利_99r_国产日韩在线视频_直接看av的网站_中文欧美日韩_久久一

您的位置:首頁技術(shù)文章
文章詳情頁

python with語句的原理與用法詳解

瀏覽:15日期:2022-07-31 17:18:19

本文實(shí)例講述了python with語句的原理與用法。分享給大家供大家參考,具體如下:

之前看到一篇博客說博主python面試時(shí)遇到面試官提問with的原理,而那位博主的博文沒有提及with原理,故有此文。

關(guān)于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)* ':' suite

with_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語句用于使用由上下文管理器定義的方法來封裝塊的執(zhí)行(請參見使用語句上下文管理器一節(jié))。 這允許通用的try…except…finally使用模式被封裝以便于重用【這句話大概意思就是“with語句”類似于try…except…finally封裝之后的的情況】。

帶有一個(gè)“項(xiàng)目”的with語句的執(zhí)行過程如下:1.上下文表達(dá)式(在with_item中給出的表達(dá)式)被評估以獲得上下文管理器。【會(huì)區(qū)分類型來處理,如文件,進(jìn)程等都可以使用with語句】2.上下文管理器的__exit __()被加載供以后使用。【負(fù)責(zé)上下文的退出】3.上下文管理器的__enter __()方法被調(diào)用。【負(fù)責(zé)上下文的進(jìn)入】4.如果在with語句中包含目標(biāo),則將__enter __()的返回值分配給它。【如果with后面跟著as 對象(如with open() as f),那么此對象獲得with上下文對象的__enter__()的返回值,(附:應(yīng)該是類似操作數(shù)據(jù)庫時(shí)的連接對象和游標(biāo)的區(qū)別)】

注意with語句保證,如果__enter __()方法返回時(shí)沒有錯(cuò)誤,那么將始終調(diào)用__exit __()。 因此,如果在分配給目標(biāo)列表期間發(fā)生錯(cuò)誤,它將被視為與套件內(nèi)發(fā)生的錯(cuò)誤相同。 請參閱下面的第6步。

5.該套件已執(zhí)行。【意思就是語句體中的過程執(zhí)行完畢,執(zhí)行完畢就到第六步--調(diào)用__exit__()來退出】6.上下文管理器的__exit __()方法被調(diào)用。 如果異常導(dǎo)致套件退出,則其類型,值和回溯作為參數(shù)傳遞給__exit __()。 否則,將提供三個(gè)無參數(shù)。

關(guān)于退出返回值:

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,則會(huì)重新對異常進(jìn)行重新評估。 如果返回值為true,則異常被抑制,并繼續(xù)執(zhí)行with語句后面的語句。

如果套件由于除了異常之外的任何原因而退出,則__exit __()的返回值將被忽略,并且執(zhí)行將在正常位置繼續(xù)進(jìn)行。

意思就是:

如果是異常退出,那么會(huì)返回false,(根據(jù)文檔中的exit的描述“that __exit__() methods should not reraise the passed-in exception; this is the caller’s responsibility.”,大概意思就是exit()不會(huì)處理異常,會(huì)重新拋出異常拋出給外面,由調(diào)用者處理,因?yàn)檫@是調(diào)用者的責(zé)任)

如果返回 True,則忽略異常,不再對異常進(jìn)行處理【(在exit內(nèi)部處理完異常后,可以讓”__exit__()”方法返回True,此時(shí)該異常就會(huì)不會(huì)再被拋出,with會(huì)認(rèn)為它的執(zhí)行體沒有發(fā)生異常)】

(with會(huì)識別返回值,根據(jù)返回值來處理,如果是False,那么with會(huì)將執(zhí)行體中的異常拋出,如果是True,那么with會(huì)認(rèn)為沒有發(fā)生異常(忽略異常),而繼續(xù)執(zhí)行外面的語句,但由于內(nèi)部調(diào)用的了__exit__(),所以在異常之后的語句是不會(huì)運(yùn)行的)

附上一個(gè)文檔中提供的一個(gè)關(guān)于with中使用鎖的例子:

python with語句的原理與用法詳解

幾個(gè)測試:1.執(zhí)行體中發(fā)生異常:

import timeclass 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 則繼續(xù)執(zhí)行 print('None:',exc_type, exc_val, exc_tb) else: #異常不為空時(shí)執(zhí)行,這一步,如果with語句體中發(fā)生異常,那么也會(huì)執(zhí)行 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:執(zhí)行體中發(fā)生異常 raise Exception('母雞啊') print('main continue')

結(jié)果顯示:python with語句的原理與用法詳解

1.拋出異常后,后面main continue不再執(zhí)行

2.__exit__()中的else會(huì)執(zhí)行

測試2:當(dāng)else中強(qiáng)制返回為True時(shí):

import timeclass 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 則繼續(xù)執(zhí)行 print('None:',exc_type, exc_val, exc_tb) else: #異常不為空時(shí)執(zhí)行,這一步,如果with語句體中發(fā)生異常,那么也會(huì)執(zhí)行 print('exception:', exc_type, exc_val, exc_tb) print('all done') return True #這里如果返回true可以看到發(fā)生異常后,main continue可以執(zhí)行 #即,如果exc_type是true,那么會(huì)繼續(xù)執(zhí)行,實(shí)際上,也可以在這里處理一下異常再返回trueif __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')#這里不會(huì)執(zhí)行 print('main continue')

結(jié)果顯示:python with語句的原理與用法詳解

1.返回True之后,with會(huì)忽略異常,繼續(xù)執(zhí)行,所以這里“main continue”能執(zhí)行

2.即使忽略異常,在with體中異常之后的語句依舊不會(huì)執(zhí)行

附:理論上可以在返回True之前處理一下異常

PS:如果大家想要了解得更詳細(xì),可以自己嘗試去讀一下官方文檔。

附上關(guān)于with語句的詳細(xì)介紹官方文檔:https://www.python.org/dev/peps/pep-0343/

更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python文件與目錄操作技巧匯總》、《Python文本文件操作技巧匯總》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門與進(jìn)階經(jīng)典教程》

希望本文所述對大家Python程序設(shè)計(jì)有所幫助。

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 精品国产91乱码一区二区三区 | 日本一区二区三区四区不卡视频 | 国产在线中文字幕 | 欧美精品欧美精品系列 | 久久国产欧美日韩精品 | 成人在线播放网站 | 精品无人乱码一区二区三区的优势 | 爱操在线 | 成人免费激情视频 | 国产高清在线精品一区二区三区 | 欧美成人激情 | 久久av一区 | 欧美国产视频 | 这里有精品视频 | 日韩欧美国产一区二区三区 | 亚洲 欧美 在线 一区 | 久久爱成人 | 成人在线国产 | 国产精品久久久久久久久久10秀 | 日穴视频在线观看 | 日韩欧美在线播放 | 精品久久一二三区 | 日韩一级在线免费观看 | 成人在线观看av | 久久久久久这里只有精品 | 亚洲一区二区免费看 | 免费av一区二区三区 | www.成人国产 | 一本大道综合伊人精品热热 | 四虎永久免费在线 | 欧美日韩一区二区在线 | 在线亚洲成人 | 日日天天| 精品综合| 一区视频网站 | 91视频三区 | 久久精品无码一区二区日韩av | 精品在线一区二区三区 | 久久精品国产99国产精品 | 欧美日韩亚洲综合 | 美女一区|