如何查看Python函數的源代碼
來自專欄 恆仔のPython
這兩天在寫code的時候,某個函數error,就想知道這個函數到底做了啥會報錯。
那當想知道一些函數的源代碼寫了什麼方法,想知道源代碼放在哪個路徑中,或者想將源代碼進行字元操作。該咋做呢?
這時候,我們可以用很簡單的方法來做Python函數源代碼的查看,Python中有兩個Libraries能夠使用:
- inspect 一個Python內置的標準庫
- drill 是一個第三方庫
inspect
inspect模塊用於收集python對象的信息,可以獲取類或函數的參數的信息,源碼,解析堆棧,對對象進行類型檢查等等
舉個栗子,用BeautifulSoup做分析,現在我們導入了兩個庫
import inspectfrom bs4 import BeautifulSoup
先看看BeautifulSoup的文檔定義
inspect.getdoc(BeautifulSoup)
輸出是:
This class defines the basic interface called by the tree builders.
These methods will be called by the parser:
reset()
feed(markup)
The tree builder may call these methods from its feed() implementation:
handle_starttag(name, attrs) # See note about return value
handle_endtag(name)
handle_data(data) # Appends to the current data node
endData(containerClass=NavigableString) # Ends the current data node
No matter how complicated the underlying parser is, you should be
able to build a tree using start tag events, end tag events,
data events, and "done with data" events.
If you encounter an empty-element tag (aka a self-closing tag,
like HTMLs <br> tag), call handle_starttag and then
handle_endtag.
再來看看BeautifulSoup存放的路徑
inspect.getsourcefile(BeautifulSoup)
輸出是
/Users/vincentyau/anaconda3/lib/python3.6/site-packages/bs4/__init__.py
重點來了,當想看源代碼的時候,可以用
inspect.getsourcelines(BeautifulSoup)
輸出是
([class BeautifulSoup(Tag):
, """
, This class defines the basic interface called by the tree builders.
,
, These methods will be called by the parser:
, reset()
, feed(markup)
,
, The tree builder may call these methods from its feed() implementation:
, handle_starttag(name, attrs) # See note about return value
, handle_endtag(name)
, handle_data(data) # Appends to the current data node
, endData(containerClass=NavigableString) # Ends the current data node
,
, No matter how complicated the underlying parser is, you should be
, " able to build a tree using start tag events, end tag events,
", data events, and "done with data" events.
,
,注釋:太長了,我就截了一小段
如果你是用Python或者Notebook的話,inspect也可以查看你自己寫的函數,用法和上面的一樣。
但如果你是用terminal之類的Python編譯,來查看自己定義的函數,則會引發IOError: could not get source code。
dill
剛剛發現在百度搜索dill,還搜索不出相關的結果,說明dill還是很小眾,那官網的相關定義是
dill extends Pythons pickle module for serializing and deserializing Python objects to the majority of the built-in Python types. At the same time, it can also retrieve the source code of your Python objects. Please note dill is not a standard library, so you must install it separately.
翻譯一下
dill擴展了Python的pickle模塊,用於序列化和反序列化Python對象到大多數內置Python類型。同時,它還可以檢索Python對象的源代碼。請注意dill不是標準庫,所以必須單獨安裝它。
不過我還發現,Anaconda已經默認安裝了dill(⊙v⊙)嗯
那基本的用法是
import dill
獲得源代碼文件路徑
dill.source.getsourcefile(BeautifulSoup)
獲得源代碼
dill.source.getsourcelines(BeautifulSoup)
以上的輸出和inspect一樣,還有findsouce()函數
dill.source.findsource(BeautifulSoup)
這個的輸出是
(["""Beautiful Soup
, Elixir and Tonic
, "The Screen-Scrapers Friend"
, http://www.crummy.com/software/BeautifulSoup/
,
, Beautiful Soup uses a pluggable XML or HTML parser to parse a
, (possibly invalid) document into a tree representation. Beautiful Soup
, provides methods and Pythonic idioms that make it easy to navigate,
, search, and modify the parse tree.
,
, Beautiful Soup works with Python 2.7 and up. It works better if lxml
, and/or html5lib is installed.
,
, For more than you ever wanted to know about Beautiful Soup, see the
, documentation:
, http://www.crummy.com/software/BeautifulSoup/bs4/doc/
,
, """
,
, # Use of this source code is governed by a BSD-style license that can be
, # found in the LICENSE file.
,注釋:太長了截一段
dill還有蠻多函數的,那dill和inspect有一個大的差別就是,你用Plain Python也能用dill查看自定義的函數,而inspect則會報錯
參考資料:
[1] How to retrieve source code of Python functions
[2] Inspect Document
[3] DILL
推薦閱讀:
※第三章 Python讀取PDF內容
※10道Python小題
※Python 的函數是怎麼傳遞參數的?
※快來看看招商銀行理財產品數據(代碼及分析)
※【小林的OpenCV基礎課 16】直方圖/一定會再次啟程