Python借助with語(yǔ)句實(shí)現(xiàn)代碼段只執(zhí)行有限次
debug的時(shí)候,有時(shí)希望打印某些東西,但是如果代碼段剛好在一個(gè)循環(huán)或者是其他會(huì)被執(zhí)行很多次的部分,那么用來(lái)print的語(yǔ)句也會(huì)被執(zhí)行很多次,看起來(lái)就不美觀。
例如:
a = 0 for i in range(3): ? ? a += 1 print(a)
這里在中間希望確認(rèn)一下a的類(lèi)型,debug的時(shí)候改成:
a = 0 for i in range(3): ? ? print(type(a)) ? ? a += 1 print(a) ''' 打印結(jié)果: <class 'int'> <class 'int'> <class 'int'> 3 '''
有3個(gè) <class ‘int’>,很不好看。
為了解決這個(gè)問(wèn)題,可以借助with語(yǔ)句實(shí)現(xiàn),首先要定義一個(gè)能夠在with語(yǔ)句中使用的類(lèi)(實(shí)現(xiàn)了__enter__和__exit__):
from typing import Any
class LimitedRun(object):
? ? run_dict = {}
? ? def __init__(self,
? ? ? ? ? ? ? ? ?tag: Any = 'default',
? ? ? ? ? ? ? ? ?limit: int = 1):
? ? ? ? self.tag = tag
? ? ? ? self.limit = limit
? ? def __enter__(self):
? ? ? ? if self.tag in LimitedRun.run_dict.keys():
? ? ? ? ? ? LimitedRun.run_dict[self.tag] += 1
? ? ? ? else:
? ? ? ? ? ? LimitedRun.run_dict[self.tag] = 1
? ? ? ? return LimitedRun.run_dict[self.tag] <= self.limit
? ? def __exit__(self, exc_type, exc_value, traceback):
? ? ? ? return
tag是標(biāo)簽,相同標(biāo)簽共用執(zhí)行次數(shù)計(jì)數(shù)器;limit是限制執(zhí)行的次數(shù)。例子如下:
a = 0
for i in range(3):
? ? with LimitedRun('print_1', 1) as limited_run:
? ? ? ? if limited_run:
? ? ? ? ? ? print(type(a))
? ? a += 1
print(a)打印結(jié)果:
<class 'int'>
3
a = 0
for i in range(3):
? ? with LimitedRun('print_1', 4) as limited_run:
? ? ? ? if limited_run:
? ? ? ? ? ? print(1, type(a))
? ? a += 1
for i in range(3):
? ? with LimitedRun('print_1', 4) as limited_run:
? ? ? ? if limited_run:
? ? ? ? ? ? print(2, type(a))
? ? a += 1
print(a)打印結(jié)果:(相同tag共用了計(jì)數(shù)器,因此總共只會(huì)執(zhí)行4次)
1 <class 'int'>
1 <class 'int'>
1 <class 'int'>
2 <class 'int'>
6
a = 0
for i in range(3):
? ? with LimitedRun('print_1', 4) as limited_run:
? ? ? ? if limited_run:
? ? ? ? ? ? print(1, type(a))
? ? a += 1
for i in range(3):
? ? with LimitedRun('print_2', 4) as limited_run:
? ? ? ? if limited_run:
? ? ? ? ? ? print(2, type(a))
? ? a += 1
print(a)打印結(jié)果:(不同tag不共用計(jì)數(shù)器)
1 <class 'int'>
1 <class 'int'>
1 <class 'int'>
2 <class 'int'>
2 <class 'int'>
2 <class 'int'>
6
到此這篇關(guān)于Python借助with語(yǔ)句實(shí)現(xiàn)代碼段只執(zhí)行有限次的文章就介紹到這了,更多相關(guān)Python代碼段執(zhí)行有限次內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python 按照sheet合并多個(gè)Excel的示例代碼(多個(gè)sheet)
這篇文章主要介紹了python 按照sheet合并多個(gè)Excel的示例代碼(多個(gè)sheet),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09
解決python父線程關(guān)閉后子線程不關(guān)閉問(wèn)題
這篇文章主要介紹了解決python父線程關(guān)閉后子線程不關(guān)閉問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04
Python+opencv+pyaudio實(shí)現(xiàn)帶聲音屏幕錄制
今天小編就為大家分享一篇Python+opencv+pyaudio實(shí)現(xiàn)帶聲音屏幕錄制,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12
pandas多級(jí)分組實(shí)現(xiàn)排序的方法
下面小編就為大家分享一篇pandas多級(jí)分組實(shí)現(xiàn)排序的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-04-04
在Python中append以及extend返回None的例子
今天小編就為大家分享一篇在Python中append以及extend返回None的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-07-07
如何實(shí)現(xiàn)在jupyter notebook中播放視頻(不停地展示圖片)
這篇文章主要介紹了如何實(shí)現(xiàn)在jupyter notebook中播放視頻(不停地展示圖片),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04
python 默認(rèn)參數(shù)相關(guān)知識(shí)詳解
這篇文章主要介紹了python 默認(rèn)參數(shù)相關(guān)知識(shí)詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09

