天天看點

inspect子產品使用

inspect子產品主要用于檔案中類型判斷等資訊提取。更詳細文檔後續更新。

參見python标準使用指南

https://docs.python.org/2/library/inspect.html#inspect.cleandoc

import inspect
import os

class Test(object):
    """Test Class """  
    def test(self):
        self.fuc = lambda x:x

class Testone(Test):
    pass

#檢查類型,子產品,類,方法,生成器,代碼等都可以  
inspect.ismodule(os)#判斷是不是module
inspect.isclass(Test)判斷是不是class

inspect.getdoc(Test)#擷取定義時的注釋說明
inspect.cleandoc(Test)#清除doc
inspect.getsourcefile(Test)#檔案路徑
inspect.getsourcelines(Test)#獲得TEST類的代碼塊,每行一個元素,組成數組;最後一個元素是代碼第一行所在的行号
inspect.getsource(Test)#獲得TEST類的代碼塊 帶縮進
           

擴充:

http://my.oschina.net/taisha/blog/55597

http://www.ziliao1.com/Article/Show/DEB3F1CB764989C6913958F2CA350DE2.html