inspect 模組
昨天在infoq上看到豆瓣架構師演講有提到這個模組 今天找來文檔看看
Doc:這樣寫到
The inspect module provides several useful functions to help get information about live objects such as modules, classes, methods, functions, tracebacks, frame objects, and code objects.
這個模組是針對模組,類,方法,功能等對象提供些有用的方法。例如可以協助我們檢查類的內容,檢查方法的代碼,提取和格式化方法的參數等。
#coding:utf8 import inspectimport os class Test(object): """Test Class """ def test(self): self.fuc = lambda x:x class Testone(Test): pass #檢查類型,模組,類,方法,產生器,代碼等都可以print inspect.ismodule(os) print inspect.isclass(Test) print inspect.getdoc(Test)print inspect.getsourcefile(Test) #檔案路徑print inspect.getsourcelines(Test) #代碼塊,每行一個元素,組成數組print inspect.getsource(Test) #代碼塊 帶縮排#列印全域變數中的模組對象myglobals = {}myglobals.update(globals())modules = [value for key, value in myglobals.items() if inspect.ismodule(value)]print modules #查看類的可調用方法for name, value in inspect.getmembers(Test, callable): print " Callable:", namefor name, value in inspect.getmembers(Test(), callable): print " Instance Callable:", namedef hello(): print inspect.stack()[0][3] print inspect.stack()hello()具體項目中的用法還不瞭解。