<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          調(diào)試Python代碼,不要再用print了!

          共 3176字,需瀏覽 7分鐘

           ·

          2021-02-06 08:51

          ↑?關(guān)注 + 星標(biāo)?,每天學(xué)Python新技能

          后臺回復(fù)【大禮包】送你Python自學(xué)大禮包


          相信大部分人學(xué)習(xí)Python,肯定會用print()這個內(nèi)置函數(shù),來調(diào)試代碼的。


          那么在一個大型的項目中,如果你也是使用print來調(diào)試你的Python代碼,你就會發(fā)現(xiàn)你的終端有多個輸出。


          那么你便不得不去分辨,每一行的輸出是哪些代碼的運行結(jié)果。


          舉個例子,運行下面這個程序。


          num1?=?30
          num2?=?40?

          print(num1)
          print(num2)


          輸出結(jié)果。


          30
          40


          這些輸出中哪一個是num1?哪一個又是num2呢?


          找出兩個輸出可能不是很困難,但是如果有五個以上的不同輸出呢?嘗試查找與輸出相關(guān)的代碼可能會很耗時。


          當(dāng)然你可以在打印語句中添加文本,使其更容易理解:


          num1?=?30
          num2?=?40?

          print("num1"?num1)
          print("num2"?num1)


          輸出結(jié)果。


          num1?30
          num2?40


          這個結(jié)果就很容易理解了,但是需要時間去寫相關(guān)的信息。


          這時就該「Icecream」上場了~



          01. 什么是Icecream?


          Icecream是一個Python第三方庫,可通過最少的代碼使打印調(diào)試更清晰明了。


          使用pip安裝Icecream庫。


          pip?install?icecream


          下面,讓我們通過打印Python函數(shù)的輸出來進行嘗試。


          from?icecream?import?ic?

          def?plus_five(num):
          ????return?num?+?5

          ic(plus_five(4))
          ic(plus_five(5))


          輸出結(jié)果如下。


          ic|?plus_five(4):?9
          ic|?plus_five(5):?10


          通過使用icecream,我們不僅可以看到函數(shù)輸出,還可以看到函數(shù)及其參數(shù)!



          02. 檢查執(zhí)行情況


          如果你想要找到執(zhí)行代碼的位置,可以通過執(zhí)行如下所示的操作,來查找執(zhí)行了哪個語句。


          def?hello(user:bool):
          ????if?user:
          ????????print("I'm?user")
          ????else:
          ????????print("I'm?not?user")

          hello(user=True)


          輸出結(jié)果。


          I'm?user


          使用icecream則無需多余的文本信息,就可以輕松地完成上述的操作。


          from?icecream?import?ic?

          def?hello(user:bool):
          ????if?user:
          ????????ic()
          ????else:
          ????????ic()

          hello(user=True)


          輸出結(jié)果如下。


          ic|?ice_1.py:5?in?hello()?at?02:34:41.391


          從輸出結(jié)果看,函數(shù)hello中的第5行的代碼已被執(zhí)行,而第7行的代碼未執(zhí)行。



          03. 自定義前綴


          如果您想在打印語句中插入自定義前綴(例如代碼執(zhí)行時間),icecream也是能實現(xiàn)的。


          from?datetime?import?datetime
          from?icecream?import?ic?
          import?time
          from?datetime?import?datetime

          def?time_format():
          ????return?f'{datetime.now()}|>?'

          ic.configureOutput(prefix=time_format)

          for?_?in?range(3):
          ????time.sleep(1)
          ????ic('Hello')


          輸出結(jié)果如下。


          2021-01-24?10:38:23.509304|>?'Hello'
          2021-01-24?10:38:24.545628|>?'Hello'
          2021-01-24?10:38:25.550777|>?'Hello'


          可以看到代碼的執(zhí)行時間,就顯示在輸出的前面。



          04. 獲取更多的信息


          除了知道和輸出相關(guān)的代碼之外,你可能還想知道代碼執(zhí)行的行和代碼文件。


          在ic.configureOutput()中,設(shè)置includeecontext的參數(shù)值為True即可。


          from?icecream?import?ic?

          def?plus_five(num):
          ????return?num?+?5

          ic.configureOutput(includeContext=True)
          ic(plus_five(4))
          ic(plus_five(5))


          輸出結(jié)果如下。


          ic|?ice_test.py:7?in?-?plus_five(4):?9
          ic|?ice_test.py:8?in?-?plus_five(5):?10


          這里我們就知道了,第一個輸出是由函數(shù)plus_five在文件icecream_example.py的第7行執(zhí)行的。


          第二個輸出則是由函數(shù)plus_five在代碼文件的第8行執(zhí)行的。

          上述兩個操作都用到了ic.configureOutput()函數(shù)。


          通過查看源碼,可知有四個可供設(shè)置的參數(shù)。

          • prefix,自定義輸出前綴

          • outputFunction,更改輸出函數(shù)

          • argToStringFunction,自定義參數(shù)序列化字符串

          • includeContext,顯示文件名、代碼行、函數(shù)信息



          05. 刪除Icecream代碼

          最后你可以將icecream僅用于調(diào)試,而將print用于其他目的(例如漂亮的打印)。

          from?icecream?import?ic

          def?plus_five(num):
          ????return?num?+?5

          ic.configureOutput(includeContext=True)
          ic(plus_five(4))
          ic(plus_five(5))

          for?i?in?range(10):
          ????print(f'******?Training?model?{i}?******')


          輸出結(jié)果。


          ic|?ice_1.py:7?in?-?plus_five(4):?9
          ic|?ice_1.py:8?in?-?plus_five(5):?10
          ******?Training?model?0?******
          ******?Training?model?1?******
          ******?Training?model?2?******
          ******?Training?model?3?******
          ******?Training?model?4?******
          ******?Training?model?5?******
          ******?Training?model?6?******
          ******?Training?model?7?******
          ******?Training?model?8?******
          ******?Training?model?9?******


          由于你可以區(qū)分調(diào)試打印和漂亮打印,因此搜索和刪除所有ic調(diào)試語句非常容易。



          刪除所有調(diào)試代碼后,你的Python代碼就整潔了。



          總結(jié)


          到此,你就應(yīng)該就學(xué)會了如何使用icecream去打印調(diào)試。


          更多功能可以訪問「GitHub」,了解詳情~


          https://github.com/gruns/icecream


          見面禮


          碼加我微信備注「三劍客」送你上圖三本Python入門電子書?


          推薦閱讀


          1. 好氣!進大廠被學(xué)歷卡住了...

          2. 熬夜一周整理我的數(shù)據(jù)分析學(xué)習(xí)資源

          3. 為什么建議大家使用 Linux 開發(fā)?爽(外加七個感嘆號)

          4. 我為什么拋棄Windows,入坑MacBook

          5. 自學(xué)Python3年,我終于做了這個決定....

          點分享
          點收藏
          點點贊
          點在看



          瀏覽 25
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  黄色一级录相 | 久久综合婷婷国产五区 | 一级片视频在线观看大全 | 亚洲免费国产 | 欧美一级黄色录像在线视频官网 |