python3 如何使用 goto 跳轉(zhuǎn)執(zhí)行到指定代碼行
1. 緣起
在實(shí)際開發(fā)中遇到這樣一個(gè)問題:
以下是偽代碼
if embedding.model is not exist: calculate embedding## moudel_1 save embedding.model else : embedding = load embedding.model try: use embedding except KeyError: calculate embedding ##這里與moudel_1一致。
發(fā)現(xiàn)except 中需要粘貼之前寫過的calculate embedding
簡單概括就是:
somecode_1 try: somecode_2 except: somecode_3 somecode_1## 重新執(zhí)行
2. 使用goto
(1)安裝goto
pip install goto-statement
(2)使用goto完成一個(gè)小例子
官方文檔見:https://pypi.org/project/goto-statement/
定義函數(shù)
from goto import with_goto @with_goto #必須有 def test(list_): tmp_list = list_ label.begin #標(biāo)識(shí)跳轉(zhuǎn)并開始執(zhí)行的地方 result = [] try: for i, j in enumerate(list_): tmp = 1 / j result.append(tmp) last_right_i = i except ZeroDivisionError: del tmp_list[last_right_i + 1] goto.begin#在有跳轉(zhuǎn)標(biāo)識(shí)的地方開始執(zhí)行 return result
運(yùn)行
a = test([1, 3, 4, 0, 6]) print(a)
結(jié)果
[1.0, 0.3333333333333333, 0.25, 0.16666666666666666]
注意:如果你在ide山運(yùn)行l(wèi)abel 和 goto 下有紅色波浪線提示錯(cuò)誤。不用理會(huì)直接執(zhí)行即可
補(bǔ)充:Pycharm跳轉(zhuǎn)回之前所在的代碼行
用Pycharm寫Python代碼有一段時(shí)間了,有一個(gè)問題一直困擾著我:瀏覽代碼的時(shí)候時(shí)常需要從一個(gè)函數(shù)跳轉(zhuǎn)到另一個(gè)函數(shù),有時(shí)候兩個(gè)函數(shù)相聚比較遠(yuǎn),我一直不知道怎么直接回到上一個(gè)函數(shù)。
于是我采取的辦法是按ctr+F然后輸入上一個(gè)函數(shù)的函數(shù)名來定位并且回到上一個(gè)函數(shù)。不忍直視~~~
下面是找到跳轉(zhuǎn)按鈕的過程:
1.找到View下面的Toolbar并勾選上

2.按鈕出現(xiàn)

一款對(duì)瀏覽代碼功能支持良好的IDE一般Toolbar上都有按鈕或者會(huì)有快捷鍵可以在歷史瀏覽頁面之間跳轉(zhuǎn)。而我的Pycharm的默認(rèn)設(shè)置一直是將Toolbar隱藏起來的。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持本站。
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。
關(guān)注官方微信